Don't expect too much from Django's admin site

Django Advent Calendar 2016 This is the article on the 12th day.

Introduction

One of the features of Django, a Python web framework, is that the administration site is a god.

管理サイトの一覧画面

The list screen and registration / edit screen with the above filters, text search, sorting, paging, etc. can be realized with just a few lines of declarative code as shown below.

python


@admin.register(Question)
class QuestionAdmin(admin.ModelAdmin):

    list_display = ('question_text', 'pub_date', 'was_published_recently')

    list_filter = ['pub_date']

    search_fields = ['question_text']

It makes me think that building a web application is very easy with this, but there are various restrictions on the management site, so don't expect too much.

Management site is for limited use

First of all, the management site is designed as a site for administrators as its name suggests. The administrator here is a so-called inside person such as a system administrator or a service operator.

The management site provides a list / registration / edit screen for each model, but this is a function for administrators and is not open to general users.

If a general user needs a screen to view / update data, it is necessary to create it on a normal screen (hereinafter referred to as a general site) instead of the management site.

For example, consider building a web application that allows you to view and register articles. If the service operator pre-registers the category to classify the articles, it may be created on the management site. However, the article viewing / registration screen used by general users will be created as a general site.

Here are some examples of use cases for each site.

--General site --General users register an account --General users view articles --General users post articles --Management site --System administrator and service operator register an administrator account --The service operator registers the category --The service operator suspends the account of a general user --The service operator deletes articles that violate the rules

The basic screen configuration is fixed

The main screen of the management site consists of the top menu and the list / registration / edit screen of each model.

The top menu is grouped by the application (the one created by startapp) and consists of links to the model.

Site_administration___Django_site_admin.png

You can add custom actions (screens) to your model's screen, but you can't significantly change the original screen configuration.

Management site functions cannot be incorporated into general sites

The following convenient functions are pre-installed on the management site list screen.

--Paging --Sort

These features will almost certainly be needed on regular sites as well. However, unfortunately, the functions of the management site cannot be diverted to general sites. You have to make it yourself.

The class-based view is easy to implement because it provides an aid to the implementation of the paging sort filter apart from the functionality of the admin site, but it still doesn't have any templates so you'll have to write it yourself.

It may be difficult or impossible to do more than expected customization

On the admin site, simple customization can be achieved with just one line of code in the Admin class. The options available in the Admin class are here (https://docs.djangoproject.com/en/1.10/ref/contrib/admin/#modeladmin-options).

Customizations other than those described here will override the methods of the Admin class. However, compared to the View classes for general sites, there are many parts that are not designed with much awareness of extension. There are some monolithic methods that have a lot of lines, and some that call private functions in the module, which can be quite difficult to override.

Finally

As I wrote a lot, the management site is a very powerful tool for administrators. There is no reason not to use it.

The benefits of using management tools

--Data can be registered and viewed without a general site --Items that are not displayed on general sites can be displayed --Data can be viewed in a state close to raw data

And so on.

Please understand the usage and restrictions of the management tool, and follow the dosage and use correctly.

Recommended Posts

Don't expect too much from Django's admin site
I wanted to modify Django's admin site a bit