0
0
Djangoframework~10 mins

Registering models in admin in Django - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Registering models in admin
Create model class
Define admin.py file
Import model in admin.py
Call admin.site.register(Model)
Run server and open admin
Model appears in admin panel
This flow shows how you create a model, import it in admin.py, register it, and then see it in the Django admin site.
Execution Sample
Django
from django.contrib import admin
from .models import Book

admin.site.register(Book)
This code imports the Book model and registers it with the Django admin site so it appears in the admin panel.
Execution Table
StepActionCode ExecutedEffect
1Import admin modulefrom django.contrib import adminadmin module ready to use
2Import Book modelfrom .models import BookBook model available in admin.py
3Register Book modeladmin.site.register(Book)Book model added to admin site registry
4Run server and open adminpython manage.py runserverAdmin panel loads with Book model listed
5ExitN/AProcess complete, model visible in admin
💡 Model registered and visible in admin panel, no further steps needed
Variable Tracker
VariableStartAfter Step 2After Step 3Final
adminNot importedImportedImportedImported
BookNot importedImportedImportedImported
admin.site._registryEmpty or existing modelsUnchangedContains Book modelContains Book model
Key Moments - 2 Insights
Why do we need to import the model in admin.py before registering it?
Because admin.site.register() needs the model class to know what to add to the admin panel. See execution_table step 2 and 3.
What happens if you forget to call admin.site.register()?
The model won't appear in the admin panel even if imported. The registration step (execution_table step 3) is essential.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the effect of step 3?
ABook model is imported
BBook model is added to admin site registry
CServer starts running
DAdmin module is imported
💡 Hint
Check the Effect column in step 3 of the execution_table
At which step does the Book model become available in admin.py?
AStep 2
BStep 3
CStep 1
DStep 4
💡 Hint
Look at the Action and Code Executed columns in execution_table step 2
If you skip step 3, what will happen when you open the admin panel?
ABook model will appear in admin
BAdmin panel will not load
CBook model will NOT appear in admin
DServer will crash
💡 Hint
Refer to key_moments question 2 and execution_table step 3
Concept Snapshot
Registering models in Django admin:
1. Import your model in admin.py
2. Call admin.site.register(ModelName)
3. Run server and open admin panel
4. Model appears for easy data management
Remember: Import + Register = Visible in admin
Full Transcript
To register a model in Django admin, first import the admin module and your model in the admin.py file. Then call admin.site.register() with your model class. This adds the model to the admin site's registry. When you run the server and open the admin panel, the registered model appears automatically. Without registering, the model won't show up even if imported. This process helps you manage your data easily through the admin interface.