Django Admin


Django Admin is a backend user interface for web application superuser and staff users. You can register your, models in the admin site and can control create, view, change and delete activities in models. A superuser can be created first with createsuperuser command and then the other users can be created from the Admin interface.


Django Admin - Users & Groups


To create your first user as superuser run:

python manage.py createsuperuser


It will then ask to input the below fields and confirm Superuser was created successfully.

    Username : 
    Email address:
    Password:
    Password (again):
                                


You may visit the admin page with the below URL (if the development server is running):

django admin loginYou can then use the aforementioned login credentials on the Django Administration page. Other Users and authentication Groups can be created with an Admin interface.


django admin loginRegister Model with Django Admin


To register models with the admin interface simply add the below statement in the admin.py module. Here Blog is an example of a Model name.


    from django.contrib import admin
    from .models import Blog
    
    admin.site.register(Blog)
                                

Django Admin Options

Django admin can offer a dashboard with many options to view and manage model data. Below are some admin options and their implications.

  • list_display: It can display a list of each record in the given order.
    list_display = ['field1', 'field2', ...]
                                    
  • list_filter: It can add filters to the model with mentioned fields
    list_filter = ['field1', 'field2', ...]
                                    
  • fields: It is used to organize detail view layout.
    fields = ['field1', 'field2', ('field3', 'field4'), ...]
                                    
  • fieldsets: It is used for sectioning the detail view.
    fieldsets = (
     (None, {
     'fields': ('field1', 'field2', ...)
     }),
     ('Availability', {
     'fields': ('field1', 'field2', ...)
     }),
    )
                                    
  • list_editable: It makes mention fields available for quick editing.
    list_editable = ['field1','field2',...]
                                    
  • search_fields: It searches the query in mentioned model fields only.
    search_fields = ['field1','field2',...]
                                    
  • exclude: It is the negative list of elements to be excluded from the admin console display.
    exclude = ['field1','field2',...]
                                    
  • ordering: It defines the tuple for ordering model data.
    ordering = ('field1', ...)
                                    
  • list_per_page: It can be used for pagination. Example: 25 rows per page
    list_per_page = 25
                                    
  • radio_fields: It can convert select fields or dropdown fields to radio fields.
    radio_fields = {"field_name": admin.HORIZONTAL, ...}