SyntaxStudy
Sign Up
Django Registering Models and Basic Admin Configuration
Django Beginner 1 min read

Registering Models and Basic Admin Configuration

Django's built-in admin interface is one of its most celebrated features. It provides a fully functional data management UI that is generated automatically from your models. To make a model manageable through the admin you register it in the app's admin.py file using admin.site.register(). For basic use cases this is all you need — Django generates list views, detail views, search, filtering, and forms automatically. Customising the admin is done by creating a ModelAdmin class. The list_display attribute controls which fields appear as columns in the list view. list_filter adds a filter sidebar with options for the specified fields. search_fields enables a search box that searches the listed fields using SQL LIKE queries. ordering sets the default sort order and prepopulated_fields auto-fills slug fields from a title field. The admin site is secured by Django's authentication system — only users with is_staff=True can access it, and individual model permissions control what each staff user can do. You create a superuser with python manage.py createsuperuser. The admin URL is typically /admin/ and is included in the root URLconf with path('admin/', admin.site.urls).
Example
# blog/admin.py — basic and customised admin registration
from django.contrib import admin
from django.utils.html import format_html
from .models import Post, Category, Tag


@admin.register(Category)
class CategoryAdmin(admin.ModelAdmin):
    list_display       = ['name', 'slug']
    prepopulated_fields = {'slug': ('name',)}
    search_fields      = ['name']


@admin.register(Tag)
class TagAdmin(admin.ModelAdmin):
    list_display        = ['name', 'slug']
    prepopulated_fields = {'slug': ('name',)}


@admin.register(Post)
class PostAdmin(admin.ModelAdmin):
    list_display        = ['title', 'author', 'category', 'status', 'published_at', 'cover_preview']
    list_filter         = ['status', 'created_at', 'category']
    search_fields       = ['title', 'body', 'author__username']
    prepopulated_fields = {'slug': ('title',)}
    raw_id_fields       = ['author']
    date_hierarchy      = 'published_at'
    ordering            = ['-published_at']
    filter_horizontal   = ['tags']

    @admin.display(description='Cover')
    def cover_preview(self, obj):
        if obj.cover_img:
            return format_html('<img src="{}" height="40">', obj.cover_img.url)
        return '-'