After creating a Django model, you can register it with the Django Admin Panel so that authorized administrators can manage its records through a web browser.
admin.py file.
Registering a model means connecting the model to Django's admin site so that authorized users can manage its records.
For example, after registering a Student model, administrators can
use the admin panel to add, edit, view, and delete student records.
Model registration makes database records available through Django's built-in administration interface.
Models are normally registered in the app's admin.py file.
students/
├── admin.py
├── models.py
├── views.py
└── apps.py
The model is imported into admin.py and then registered.
Start by importing Django's admin module.
from django.contrib import admin
This gives access to Django's admin registration and customization features.
Next, import the model from the current app.
from .models import Student
The dot in .models refers to the current Django app.
The simplest way to register a model is:
from django.contrib import admin
from .models import Student
admin.site.register(Student)
This makes the Student model available in the admin interface.
Suppose models.py contains:
from django.db import models
class Student(models.Model):
name = models.CharField(
max_length=100
)
email = models.EmailField()
age = models.IntegerField()
def __str__(self):
return self.name
This model can now be registered in admin.py.
Open students/admin.py:
from django.contrib import admin
from .models import Student
admin.site.register(Student)
After registering the model, it can appear in the admin site when an authorized user logs in.
Before working with model records, make sure the model's migrations have been created and applied.
python manage.py makemigrations
python manage.py migrate
This creates or updates the database structure according to the model.
Create an administrative user if you do not already have one.
python manage.py createsuperuser
Follow the prompts to create the account.
Start the Django development server:
python manage.py runserver
Then open:
http://127.0.0.1:8000/admin/
Enter the username and password of an authorized Django user.
After successful login, the admin homepage displays the applications and models available to that user.
After registration, open the model from the admin panel and choose the option to add a new record.
For the Student model, you may see fields such as:
Enter the required information and save the record.
The registered model has a list page in the admin panel where existing records can be viewed.
The information shown in the list can be customized using
ModelAdmin.
Click an existing record to open its edit form.
Change the required fields and save the record. Django then updates the corresponding database row.
Registered model records can also be deleted through the admin interface if the user has the required permission.
Multiple models can be registered in the same admin.py file.
from django.contrib import admin
from .models import Student, Course
admin.site.register(Student)
admin.site.register(Course)
Both models can then appear in the admin interface.
Instead of registering a model directly, you can create a
ModelAdmin class to customize the admin interface.
from django.contrib import admin
from .models import Student
class StudentAdmin(admin.ModelAdmin):
pass
admin.site.register(
Student,
StudentAdmin
)
The list_display option controls which fields are displayed on
the model's list page.
class StudentAdmin(admin.ModelAdmin):
list_display = (
'name',
'email',
'age',
)
admin.site.register(
Student,
StudentAdmin
)
The search_fields option adds a search box for selected fields.
class StudentAdmin(admin.ModelAdmin):
search_fields = (
'name',
'email',
)
Administrators can search for records using these fields.
The list_filter option adds filtering controls to the list page.
class StudentAdmin(admin.ModelAdmin):
list_filter = (
'is_active',
)
This is useful for quickly narrowing down records.
The ordering option controls the default order of records.
class StudentAdmin(admin.ModelAdmin):
ordering = (
'name',
)
Use a minus sign for descending order.
ordering = (
'-id',
)
The readonly_fields option prevents selected fields from being
edited through the admin form.
class StudentAdmin(admin.ModelAdmin):
readonly_fields = (
'created_at',
)
This can be useful for automatically generated timestamps.
The fieldsets option can organize fields into sections on the
admin edit page.
class StudentAdmin(admin.ModelAdmin):
fieldsets = (
('Student Information', {
'fields': (
'name',
'email',
)
}),
('Other Information', {
'fields': (
'age',
)
}),
)
Django also provides the @admin.register decorator as another
way to register a model with a custom ModelAdmin class.
from django.contrib import admin
from .models import Student
@admin.register(Student)
class StudentAdmin(admin.ModelAdmin):
list_display = (
'name',
'email',
'age',
)
This combines registration and customization in one declaration.
Registering a model does not mean every user can perform every operation. Django's permission system controls access to actions such as viewing, adding, changing, and deleting model records.
admin.pyINSTALLED_APPS when requiredlist_displaysearch_fieldsBasic registration:
from django.contrib import admin
from .models import Student
admin.site.register(Student)
Customized registration:
from django.contrib import admin
from .models import Student
@admin.register(Student)
class StudentAdmin(admin.ModelAdmin):
list_display = (
'name',
'email',
)
search_fields = (
'name',
'email',
)
Use customization when the default admin display does not provide the functionality your project needs.
models.py
from django.db import models
class Student(models.Model):
name = models.CharField(
max_length=100
)
email = models.EmailField()
age = models.IntegerField()
is_active = models.BooleanField(
default=True
)
def __str__(self):
return self.name
admin.py
from django.contrib import admin
from .models import Student
@admin.register(Student)
class StudentAdmin(admin.ModelAdmin):
list_display = (
'name',
'email',
'age',
'is_active',
)
search_fields = (
'name',
'email',
)
list_filter = (
'is_active',
)
ordering = (
'name',
)
Commands:
python manage.py makemigrations
python manage.py migrate
python manage.py createsuperuser
python manage.py runserver
After logging into the admin site, the Student model can be managed according to the user's permissions.
Registering models is an important step when using Django's built-in admin interface. Once a model is registered, Django can generate an administrative interface for managing its records.
admin.py.admin.site.register(Model) for simple registration.ModelAdmin for customization.list_display controls list columns.search_fields adds search functionality.list_filter adds filtering functionality.ordering controls default ordering.@admin.register() provides decorator-based registration.admin.py file.admin.site.register(Student) for basic registration.ModelAdmin to customize the admin interface.list_display controls the fields shown in the record list.search_fields provides search functionality.list_filter provides filtering functionality.ordering controls the default record order.readonly_fields makes selected fields read-only.@admin.register(Model) can be used for decorator-based registration.Question: Which code is used for basic registration of a Django model in the admin panel?