Django admin site management summary

Source: Internet
Author: User

1. Using the admin site
The admin module is in Django. contrib. Therefore, you must remove the following two comments from ULRs when adding admin management;
From Django. contrib import Admin
Admin. autodiscover ()

2. Setting settings
1) Add Django. contrib. Admin to installed_apps
Tip: intalled_apps should be arranged by alphabetical, which is easier to read.

2) ensure that the following three apps are installed
Django. contrib. Sessions
Django. contrib. contenttypes
Django. contrib. auth

3) Open middleware_classes
Django. Middleware. Common. commonmiddleware
Django. contrib. Sessions. Middleware. sessionmiddleware
Django. contrib. Auth. Middleware. authenticationmiddleware

4) install the admin Database
If the above are newly added, you need to add the amdin management database.
Python manage. py syncdb

5) createsuperuser
If no superuser is created during database installation, run the following command to create
Python manage. py createsuperuser

3. Adding your models to the admin site
Create the admin. py file in the books app directory. Note that this file name can only be called Admin. py.
In the Django house, the admin. autodiscover () function is executed in URLs. py.
And find the amdin. py file name under the corresponding app. Run Admin. py.
E.g.
From Django. contrib import Admin
From website. Books Import Models

Admin. Site. Register (models. publisher)
Admin. Site. Register (models. Author)
Admin. Site. Register (models. Book)

4. Modify the Editable admin attributes.

The admin determines whether the field is required based on the non-null attribute during database creation.
The default fields are required. If you can leave them blank, you can add attributes by yourself. E.g.
Class author (models. Model ):
First_name = models. charfield (max_length = 30)
Last_name = models. charfield (max_length = 40)
Email = models. emailfield (blank = true)
Note: For time and number conditions that are allowed to be null, blank = true, null = true. Otherwise, an error is thrown.
Models. datefield (blank = true, null = true)
Here, blank = true does not change the database attribute, but null = true changes the database attribute, so
After adding null = true, you need to change the database. This is described in the previous section and needs to be modified using database commands.

5. Customizing field labels
By default, the field name of the database is displayed in Admin as the database definition name, for example, first_name in the preceding example.
The display is first_name, which can be changed to the name (Label) displayed in the database, for example:
Class author (models. Model ):
First_name = models. charfield (max_length = 30, verbose_name = "name ")
Last_name = models. charfield (max_length = 40)
Email = models. emailfield (blank = true)

Def _ Unicode _ (Self ):
Return self. last_name
6. Customizing modeladmin class
1) by default, amdin displays the content of a table defined in the _ Unicode _ function. For example
In the author table, only the content of the last_name column is displayed in Admin.
Similar to the following:
Author # Table Name
Xiuqi # last_name of the author
Yogngao

2) obviously this looks ugly. We can completely customize the display format, including modifying the label name mentioned in 5.
EXAMPLE After modification:
# Admin. py
From Django. contrib import Admin
From mysite. Books. Models import publisher, author, book

Class authoradmin (Admin. modeladmin ):
List_display = ('first _ name', 'last _ name', 'email ')

Admin. Site. Register (publisher)
Admin. Site. Register (author, authoradmin)
Admin. Site. Register (book)
# Models. py
Class author (models. Model ):
First_name = models. charfield (max_length = 30, verbose_name = "name ")
Last_name = models. charfield (max_length = 40)
Email = models. emailfield (blank = true)
The modified representation is as follows:
Name last_name email
Zhang San xxx@mail.com
Sun yonggao xxx@mail.com
3) Other custom Projects
Search_fields = ('first _ name', 'last _ name ')
List_filter = ('first _ name ',)
Date_hierarchy = ('first _ name ')
Ordering = ('-first_name ',)
Fields = ('Last _ name', 'First _ name', 'email ',) # order of customized Columns
# The fields attribute can also be used to control editable domains.
# It can be left empty.

7. Control User Group Permissions
You can directly configure this configuration.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.