django
  1. django-admin-interface

Admin Interface - Advanced Django Concepts

Syntax

from django.contrib import admin
from .models import ExampleModel

class ExampleModelAdmin(admin.ModelAdmin):
    list_display = ('field_1', 'field_2', 'field_3')
    list_filter = ('field_1', 'field_2', 'field_3')
    search_fields = ('field_1', 'field_2', 'field_3')
    list_per_page = 25

Example

Let's assume we have a models.py file with the following model:

class ExampleModel(models.Model):
    field_1 = models.CharField(max_length=100)
    field_2 = models.CharField(max_length=100)
    field_3 = models.IntegerField()

    def __str__(self):
        return f"{self.field_1} - {self.field_2} - {self.field_3}"

To define the admin interface for this model, we would create an admin.py file with the following code:

from django.contrib import admin
from .models import ExampleModel

class ExampleModelAdmin(admin.ModelAdmin):
    list_display = ('field_1', 'field_2', 'field_3')
    list_filter = ('field_1', 'field_2', 'field_3')
    search_fields = ('field_1', 'field_2', 'field_3')
    list_per_page = 25

admin.site.register(ExampleModel, ExampleModelAdmin)

Output

The output of the above example would be a user interface for managing ExampleModel instances. The user interface would display a list of objects with columns for each field (field_1, field_2, and field_3) and provide a search box with filters for each field.

Explanation

The admin interface is a powerful feature of Django that allows us to quickly and easily manage our database records through a graphical user interface. By default, Django will generate an admin interface for all of our models. However, we can customise this interface by creating an admin.py file and defining an admin class for our model.

In the example code, we define an ExampleModelAdmin class that inherits from admin.ModelAdmin. We define various attributes on this class that customise the user interface:

  • list_display: This attribute is a tuple of field names to display in the list view of the admin interface.

  • list_filter: This attribute is a tuple of field names to use for filtering the list view of the admin interface.

  • search_fields: This attribute is a tuple of field names to search for in the admin interface.

  • list_per_page: This attribute determines how many objects should be displayed per page in the admin interface.

We then register our customised admin class with the ExampleModel model by passing both classes as arguments to admin.site.register().

Use

We can use the admin interface to easily manage our database records without writing any coding. We can create, delete and edit records. The search box is especially useful when managing a large number of records. Advanced users can customise the interface to create more advanced features like inline editing of fields.

Important Points

  • The admin interface is a powerful feature of Django that allows us to manage our database records through a graphical user interface.

  • We can customise the admin interface by creating an admin.py file and defining an admin class for our model.

  • We define various attributes on the admin class to customise the user interface such as list_display, list_filter, search_fields and list_per_page.

  • Advanced users can customise the interface to create more advanced features like inline editing of fields.

Summary

The admin interface is a powerful tool for managing database records in Django. By customising the admin.py file with an admin.ModelAdmin class, we can create a user interface that perfectly fits our needs. With features like list views and search boxes, it makes managing large amounts of data a breeze.

Published on: