Lesson 28 of 60 – Register Models in Django Admin
47%

Register Models in Django Admin

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.

Note: Creating a model does not automatically make it available in the Django Admin interface. The model normally needs to be registered in the app's admin.py file.

1. What Does Registering a Model Mean?

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.

2. Why Register Models?

Model registration makes database records available through Django's built-in administration interface.

  • Add records
  • View records
  • Edit records
  • Delete records
  • Search records
  • Filter records
  • Customize record display

3. Where Are Models Registered?

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.

4. Import the Admin Module

Start by importing Django's admin module.

from django.contrib import admin

This gives access to Django's admin registration and customization features.

5. Import the Model

Next, import the model from the current app.

from .models import Student

The dot in .models refers to the current Django app.

6. Basic Model Registration

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.

7. Example Student Model

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.

8. Register the Student Model

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.

9. Run Migrations First

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.

10. Create a Superuser

Create an administrative user if you do not already have one.

python manage.py createsuperuser

Follow the prompts to create the account.

11. Start the Development Server

Start the Django development server:

python manage.py runserver

Then open:

http://127.0.0.1:8000/admin/

12. Login to Django 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.

13. Add a Model Record

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:

  • Name
  • Email
  • Age

Enter the required information and save the record.

14. View Model Records

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.

15. Edit a Model Record

Click an existing record to open its edit form.

Change the required fields and save the record. Django then updates the corresponding database row.

16. Delete a Model Record

Registered model records can also be deleted through the admin interface if the user has the required permission.

Warning: Deleting a record can permanently remove database data. Always verify the record before confirming a destructive action.

17. Register Multiple Models

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.

18. ModelAdmin Class

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
)

19. Using list_display

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
)

20. Using search_fields

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.

21. Using list_filter

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.

22. Using ordering

The ordering option controls the default order of records.

class StudentAdmin(admin.ModelAdmin):

    ordering = (
        'name',
    )

Use a minus sign for descending order.

ordering = (
    '-id',
)

23. Using readonly_fields

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.

24. Customizing Fieldsets

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',
            )
        }),
    )

25. Registering with @admin.register

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.

26. Registering Models with Permissions

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.

  • View
  • Add
  • Change
  • Delete

27. Common Registration Errors

  • Forgetting to import the model
  • Registering the wrong model name
  • Putting the code in the wrong app's admin.py
  • Forgetting to add the app to INSTALLED_APPS when required
  • Not running migrations for newly created models
  • Using an incorrect field name in list_display
  • Using an incorrect field name in search_fields

28. Basic vs Customized Registration

Basic 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.

29. Complete Model Registration Example

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.

30. Summary of Registering Models

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.

  • Register models in admin.py.
  • Import the model before registering it.
  • Use admin.site.register(Model) for simple registration.
  • Use 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.
  • Django permissions control what authorized users can do.

📌 Key Points

  • Models are registered in the app's admin.py file.
  • Use admin.site.register(Student) for basic registration.
  • Use 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.
  • Permissions determine which operations an admin user can perform.

🧠 Quick Quiz

Question: Which code is used for basic registration of a Django model in the admin panel?