← Back to Django Course | Chapter 8: Django Admin | Lesson 3 of 8

Registering Models in the Admin

Registering a model tells Django 'show this one in the admin dashboard too'.

The Simplest Registration

Calling admin.site.register() with a model class is enough to get a fully working list/add/edit/delete interface for it, using Django's defaults.

Example: The Simplest Registration

This single line makes Book manageable from the admin dashboard.

markup
from django.contrib import admin
from .models import Book

admin.site.register(Book)
{# Django-only code -- models.py/views.py/urls.py/settings.py snippets, or template markup using Django template tags/variables -- can't run standalone via Judge0 or the browser preview, since it needs a real Django project. Only this course's pure-Python examples (example_lang == 'python', no Django imports) are actually runnable, so those still get the button below. #}

Registering Multiple Models

You can register several models from the same app in one admin.py file, each with its own call.

Example: Registering Multiple Models

Both Book and Author now appear as separate sections in the admin dashboard.

markup
from django.contrib import admin
from .models import Book, Author

admin.site.register(Book)
admin.site.register(Author)
{# Django-only code -- models.py/views.py/urls.py/settings.py snippets, or template markup using Django template tags/variables -- can't run standalone via Judge0 or the browser preview, since it needs a real Django project. Only this course's pure-Python examples (example_lang == 'python', no Django imports) are actually runnable, so those still get the button below. #}

Using the @admin.register Decorator

Instead of calling register() separately, you can decorate a ModelAdmin class with @admin.register(Model) -- useful once you start customizing the admin, covered in later topics.

Warning: Registering the same model twice (e.g. once via decorator and once via admin.site.register) raises django.contrib.admin.sites.AlreadyRegistered.

Example: Using the @admin.register Decorator

This registers Book using an empty ModelAdmin, equivalent to the plain register() call but ready for customization.

markup
from django.contrib import admin
from .models import Book

@admin.register(Book)
class BookAdmin(admin.ModelAdmin):
    pass
{# Django-only code -- models.py/views.py/urls.py/settings.py snippets, or template markup using Django template tags/variables -- can't run standalone via Judge0 or the browser preview, since it needs a real Django project. Only this course's pure-Python examples (example_lang == 'python', no Django imports) are actually runnable, so those still get the button below. #}
Common Mistakes
  1. Registering the same model twice, which raises an AlreadyRegistered error.
  2. Registering a model in the wrong app's admin.py where Django never imports it.
  3. Forgetting the app must also be listed in INSTALLED_APPS for its admin.py to be loaded at all.
Chapter Summary
  • admin.site.register(Model) is the simplest way to expose a model in the dashboard.
  • The @admin.register(Model) decorator does the same thing while defining a ModelAdmin class in one step.
  • Registration always happens in the owning app's admin.py file.
  • Every INSTALLED_APPS app's admin.py is auto-discovered and loaded by Django on startup.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.