Registering Models in the Admin
In this page:
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.
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.
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.
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. #}
- Registering the same model twice, which raises an AlreadyRegistered error.
- Registering a model in the wrong app's admin.py where Django never imports it.
- Forgetting the app must also be listed in INSTALLED_APPS for its admin.py to be loaded at all.
- 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.
Chapter Quiz — Complete all 8 topics to unlock
0/8 topics done
Complete these topics first: