Skip to content

Administrator panel

Theory

So far, we have used the default administrator panel. However, we can very much adapt it to our own needs. To do this, we must:

  • reload class ModelAdmin from module django.contrib.admin

  • reloaded class should be registered for a given model with admin.site.register

The usual reloading of the ModelAdmin classes is done in the admin.py file.

List view

from django.contrib.admin import ModelAdmin


class MovieAdmin(ModelAdmin):

  @staticmethod
  def released_year(obj):
    return obj.released.year

  @staticmethod
  def cleanup_description(modeladmin, request, queryset):
    queryset.update(description=None)

  ordering = ['id']
  list_display = ['id', 'title', 'genre', 'released_year']
  list_display_links = ['id', 'title']
  list_per_page = 20
  list_filter = ['genre']
  search_fields = ['title']
  actions = ['cleanup_description']

Reloaded fields are a configuration of the following settings:

  • ordering indicates which fields are to determine the order of records in the table

  • list_display specifies the fields (and their order) displayed as columns

    • released_year is the name of the function that converts a record to a movie's release year
  • list_display_links places links to movie editing forms in given columns

  • list_per_page limits the number of records on each page

  • list_filter introduces a panel with record filters for individual values of a given field

  • search_fields adds a chain search engine in the given field

  • actions allows you to define actions for selected records

    • cleanup_description is the name of the function resetting the descriptions of movies in the given file

Form view

class MovieAdmin(ModelAdmin):
  fieldsets = [
    (None, {'fields': ['title', 'created']}),
    (
      'External Information',
      {
        'fields': ['genre', 'released'],
        'description': (
          'These fields are going to be filled with data parsed '
          'from external databases.'
        )
      }
    ),
    (
      'User Information',
      {
        'fields': ['rating', 'description'],
        'description': 'These fields are intended to be filled in by our users.'
      }
    )
  ]
  readonly_fields = ['created']

Reloaded fields are a configuration of the following settings:

  • fieldset allows you to define your own form layout

    • First field: None or string

      • An entry with a None element in the first position leaves the section without a header

      • When we enter a value of type string in the first field, we will see it in the section header

    • Second field: dict

      • The value under the fields key indicates the names of the fields in the given section

        • The created field as autoadd_now = True would not be displayed in the form, but by including fields in the list, we will be able to see it in our form
      • We will see the value under the description key as an explanation under the section title

  • readonly_fields will display the listed fields without any widgets to change them

Registration

The registration of your own admin panel for our models remains in the urls.py file. Apart from the model, we need to indicate which reload of the ModelAdmin class contains the configuration of the list and forms.

from viewer.admin import MovieAdmin
admin.site.register(Movie, MovieAdmin)