ISOM 350

Business Application Development

Mohammad AlMarzouq

Django Admin

What is the Django Admin

  • Web interface that allows the web application developer/admin to manage data
  • Provides simple CRUD functionality to all defined models
  • Must be configured by the web developer to be enabled

Configuring the Django Admin

  1. Models must be defined in your app
  2. Configure database and perform migrations
  3. Create a superuser
  4. Register model to be managed by admin in admin.py
  5. Optionally configure the admin for the model in admin.py

Creating a super user

Run the following management command in shell then follow instructions:

python manage.py createsuperuser

Registering a Model with Admin.py

  1. Import the model
  2. Pass it to admin.site.register
  3. Repeat for every model you want to manage in admin
from django.contrib import admin

from .models import Post

admin.site.register(Post)

Configuring the Admin

  • Admin interface is more useful if properly configured
  • Configuration will require defining a configuration class in admin.py
  • Class provides information to Django on:
    • How a list of objects will be displayed
    • How single object can be created/viewed

Creating an Empty Admin Configuration Class

from django.contrib import admin
from .models import Post

class PostAdmin(admin.ModelAdmin):
  pass

admin.site.register(Post, PostAdmin) 

Alternative Method

from django.contrib import admin
from .models import Post

@admin.register(Post)
class PostAdmin(admin.ModelAdmin):
  pass

Result

Simple Post Model List
Simple Post Model List

Improve Display Label for Post

class Post(models.Model):
  STATUS = (
    (0,"Draft"),
    (1,"Publish")
  )

  title = models.CharField(max_length=200, unique=True)
  slug = models.SlugField(max_length=200, unique=True)
  body = models.TextField()
  created_on = models.DateTimeField(auto_now_add=True)
  updated_on = models.DateTimeField(auto_now=True)
  status = models.IntegerField(choices=STATUS, default=0)

  def __str__(self):
    return self.title

Result

Improved Post Title Model List
Improved Post Title Model List

Configuring Displayed Columns

from django.contrib import admin
from .models import Post

@admin.register(Post)
class PostAdmin(admin.ModelAdmin):
  list_display = ('title', 'slug', 'status','created_on')

Result

Improved Admin Post List
Improved Admin Post List

Filter Option

from django.contrib import admin
from .models import Post

@admin.register(Post)
class PostAdmin(admin.ModelAdmin):
  list_display = ('title', 'slug', 'status','created_on')
  list_filter = ("status",)

Result

Admin Post Filter
Admin Post Filter

Adding search Option

from django.contrib import admin
from .models import Post

@admin.register(Post)
class PostAdmin(admin.ModelAdmin):
  list_display = ('title', 'slug', 'status','created_on')
  list_filter = ("status",)
  search_fields = ['title', 'content']

Result

Improved Admin Post List with Search
Improved Admin Post List with Search

Adding Slug Prepopulation

from django.contrib import admin
from .models import Post

@admin.register(Post)
class PostAdmin(admin.ModelAdmin):
  list_display = ('title', 'slug', 'status','created_on')
  list_filter = ("status",)
  search_fields = ['title', 'content']
  prepopulated_fields = {'slug': ('title',)}

What About Relationships?

class Author(models.Model):
    first_name = models.CharField(max_length=50)
    last_name = models.CharField(max_length=50)
    country = models.CharField(max_length=100)

class Book(models.Model):
    author = models.ForeignKey(
      'Author', on_delete=models.CASCADE)
    title = models.CharField(max_length=100)
    release_date = models.DateField()
    num_stars = models.IntegerField()

Use Inline Admins

from .models import Author, Book

class BookInline(admin.TabularInline):
    model = Book

@admin.register(Author)
class AuthorAdmin(admin.ModelAdmin):
    inlines = [
        BookInline,
    ]

Why not register Book Admin?

Result

Book Inlined as Table Within Author
Book Inlined as Table Within Author

Stacked Inline

from .models import Author, Book

class BookInline(admin.StackedInline):
    model = Book

@admin.register(Author)
class AuthorAdmin(admin.ModelAdmin):
    inlines = [
        BookInline,
    ]

Result

Book Inlined as Stack Within Author
Book Inlined as Stack Within Author

Other Important Configurations to Investigate

  • exclude, fields
  • list_display, list_display_links
  • readonly_fields, search_fields
  • max_num, min_num (InlineModelAdmin only)
  • Admin Actions (Advanced)

Admin Reference