Django Admin Management Tool
Django provides web-based management tools.
Django's automatic management tool is part of django.contrib. You can see it in INSTALLED_APPS in the project's settings.py:
/HelloWorld/HelloWorld/settings.py file code:
INSTALLED_APPS
=
(
'
django.contrib.admin
'
,
'
django.contrib.auth
'
,
'
django.contrib.contenttypes
'
,
'
django.contrib.sessions
'
,
'
django.contrib.messages
'
,
'
django.contrib.staticfiles
'
,
)
django.contrib is a large set of features and an integral part of Django's base code.
Activate the management tool
Usually we set it up automatically in urls.py when generating the project; we just need to uncomment it.
The configuration item is as follows:
/HelloWorld/HelloWorld/urls.py file code:
urls.py
from
django
.
conf
.
urls
import
url
from
django
.
contrib
import
admin
urlpatterns
=
[
url
(
r
'
^admin/
'
,
admin
.
site
.
urls
)
,
]
Once all this is configured, the Django management tool can run.
Use the management tool
Start the development server, then visit http://127.0.0.1:8000/admin/ in your browser to get the following interface:
You can use the command python manage.py createsuperuser to create a superuser, as follows:
# python manage.py createsuperuser
Username (leave blank to use 'root'): admin
Email address: admin@runoob.com
Password:
Password (again):
Superuser created successfully.
[root@solar HelloWorld]#
Then enter the username and password to log in, and the interface is as follows:
In order for the admin interface to manage a data model, we first need to register that data model with admin. For example, we created the model Test in TestModel earlier. Modify TestModel/admin.py:
HelloWorld/TestModel/admin.py: file code:
from
django
.
contrib
import
admin
from
TestModel
.
models
import
Test
Register your models here.
admin
.
site
.
register
(
Test
)
After refreshing you can see the Testmodel data table:
Complex models
The management page is powerful and fully capable of handling more complex data models.
First add a more complex data model in TestModel/models.py:
HelloWorld/TestModel/models.py: file code:
from
django
.
db
import
models
Create your models here.
class
Test
(
models
.
Model
)
:
name
=
models
.
CharField
(
max_length
=
20
)
class
Contact
(
models
.
Model
)
:
name
=
models
.
CharField
(
max_length
=
200
)
age
=
models
.
IntegerField
(
default
=
0
)
=
models
.
EmailField
(
)
def
unicode
(
self
)
:
return
self
.
name
class
Tag
(
models
.
Model
)
:
contact
=
models
.
ForeignKey
(
Contact
,
on_delete
=
models
.
CASCADE
,
)
name
=
models
.
CharField
(
max_length
=
50
)
def
unicode
(
self
)
:
return
self
.
name
There are two tables here. Tag uses Contact as a foreign key. One Contact can correspond to multiple Tags.
We can also see many attribute types we haven't seen before, such as IntegerField for storing integers.
Register multiple models in TestModel/admin.py and display them:
HelloWorld/TestModel/admin.py: file code:
from
django
.
contrib
import
admin
from
TestModel
.
models
import
Test
,
Contact
,
Tag
Register your models here.
admin
.
site
.
register
(
[
Test
,
Contact
,
Tag
]
)
Refresh the management page, and the result is as follows:
In the above management tool we can perform complex model operations.
If you have not created the table structure before, you can use the following command to create it:
$ python manage.py makemigrations TestModel # let Django know that we have some changes in our models $ python manage.py migrate TestModel # create the table structure
Custom forms
We can customize the management page to replace the default page. For example, the "add" page above. We want to display only the name and email parts. Modify TestModel/admin.py:
HelloWorld/TestModel/admin.py: file code:
from
django
.
contrib
import
admin
from
TestModel
.
models
import
Test
,
Contact
,
Tag
Register your models here.
class
ContactAdmin
(
admin
.
ModelAdmin
)
:
fields
=
(
'
name
'
,
'
'
)
admin
.
site
.
register
(
Contact
,
ContactAdmin
)
admin
.
site
.
register
(
[
Test
,
Tag
]
)
The above code defines a ContactAdmin class to specify the display format of the management page.
The fields attribute inside defines the fields to be displayed.
Since this class corresponds to the Contact data model, when registering we need to register them together. The display effect is as follows:
We can also divide the input fields into sections, each of which can define its own format. Modify TestModel/admin.py to:
HelloWorld/TestModel/admin.py: file code:
from
django
.
contrib
import
admin
from
TestModel
.
models
import
Test
,
Contact
,
Tag
Register your models here.
class
ContactAdmin
(
admin
.
ModelAdmin
)
:
fieldsets
=
(
[
'
Main
'
,{
'
fields
'
:
(
'
name
'
,
'
'
)
, }
]
,
[
'
Advance
'
,{
'
classes
'
:
(
'
collapse
'
,
)
,
CSS
'
fields
'
:
(
'
age
'
,
)
, }
]
)
admin
.
site
.
register
(
Contact
,
ContactAdmin
)
admin
.
site
.
register
(
[
Test
,
Tag
]
)
The above sections are divided into two parts: Main and Advance. classes describes the CSS format of the section it is in. Here the Advance part is hidden:
There is a Show button next to the Advance part, used to expand it; after expanding you can click Hide to hide it, as shown below:
Inline display
The Contact above is the foreign key of Tag, so there is a foreign reference relationship.
In the default page display, the two are separated, which cannot reflect their subordinate relationship. We can use inline display to have Tag displayed attached to the Contact edit page.
Modify TestModel/admin.py:
HelloWorld/TestModel/admin.py: file code:
from
django
.
contrib
import
admin
from
TestModel
.
models
import
Test
,
Contact
,
Tag
Register your models here.
class
TagInline
(
admin
.
TabularInline
)
:
model
=
Tag
class
ContactAdmin
(
admin
.
ModelAdmin
)
:
inlines
=
[
TagInline
]
Inline
fieldsets
=
(
[
'
Main
'
,{
'
fields
'
:
(
'
name
'
,
'
'
)
, }
]
,
[
'
Advance
'
,{
'
classes
'
:
(
'
collapse
'
,
)
,
'
fields
'
:
(
'
age
'
,
)
, }
]
)
admin
.
site
.
register
(
Contact
,
ContactAdmin
)
admin
.
site
.
register
(
[
Test
]
)
The display effect is as follows:
Display on the list page
After entering several records in Contact, the Contact list page looks as follows:
We can also customize the display of this page, for example, display more columns in the list, just add the list_display attribute in ContactAdmin:
HelloWorld/TestModel/admin.py: file code:
from
django
.
contrib
import
admin
from
TestModel
.
models
import
Test
,
Contact
,
Tag
Register your models here.
class
TagInline
(
admin
.
TabularInline
)
:
model
=
Tag
class
ContactAdmin
(
admin
.
ModelAdmin
)
:
list_display
=
(
'
name
'
,
'
age
'
,
'
'
)
list
inlines
=
[
TagInline
]
Inline
fieldsets
=
(
[
'
Main
'
,{
'
fields
'
:
(
'
name
'
,
'
'
)
, }
]
,
[
'
Advance
'
,{
'
classes
'
:
(
'
collapse
'
,
)
,
'
fields
'
:
(
'
age
'
,
)
, }
]
)
admin
.
site
.
register
(
Contact
,
ContactAdmin
)
admin
.
site
.
register
(
[
Test
]
)
Refresh the page and the display effect is as follows:
The search function is very useful when managing a large number of records. We can use search_fields to add a search bar to this list page:
HelloWorld/TestModel/admin.py: file code:
from
django
.
contrib
import
admin
from
TestModel
.
models
import
Test
,
Contact
,
Tag
Register your models here.
class
TagInline
(
admin
.
TabularInline
)
:
model
=
Tag
class
ContactAdmin
(
admin
.
ModelAdmin
)
:
list_display
=
(
'
name
'
,
'
age
'
,
'
'
)
list
search_fields
=
(
'
name
'
,
)
inlines
=
[
TagInline
]
Inline
fieldsets
=
(
[
'
Main
'
,{
'
fields
'
:
(
'
name
'
,
'
'
)
, }
]
,
[
'
Advance
'
,{
'
classes
'
:
(
'
collapse
'
,
)
,
'
fields
'
:
(
'
age
'
,
)
, }
]
)
admin
.
site
.
register
(
Contact
,
ContactAdmin
)
admin
.
site
.
register
(
[
Test
]
)
In this example we searched for the record with name runoob, and the result is as follows:
Django Admin Management Tool has many more useful features; interested readers can study it further.