← Back to Python

All Topics

Advertisement

Learn/Python/Web Development

Django Admin

Topic: Django

Advertisement

Introduction

Django Admin provides an automatic admin interface for managing models.

Admin Registration

# app/admin.py
from django.contrib import admin
from .models import Author, Book

@admin.register(Author)
class AuthorAdmin(admin.ModelAdmin):
    list_display = ["name", "email", "created_at"]
    search_fields = ["name", "email"]
    list_filter = ["created_at"]
    ordering = ["name"]

@admin.register(Book)
class BookAdmin(admin.ModelAdmin):
    list_display = ["title", "author", "price"]
    list_select_related = ["author"]
    date_hierarchy = "published_date"

Admin Customization

# Inline models
class BookInline(admin.TabularInline):
    model = Book
    extra = 1

class AuthorAdmin(admin.ModelAdmin):
    inlines = [BookInline]

Actions

class BookAdmin(admin.ModelAdmin):
    actions = ["mark_published", "mark_draft"]
    
    @admin.action(description="Mark as published")
    def mark_published(self, request, queryset):
        queryset.update(status="published")

Practice Problems

  1. Register models in admin
  2. Customize list display
  3. Add search and filters
  4. Use inlines for related objects
  5. Create custom admin actions

Advertisement

Advertisement

Need More Practice?

Get personalized Python help from ChatWhole's AI-powered platform.

Get Expert Help →