- Django 2 by Example
- Antonio Melé
- 450字
- 2021-06-25 21:19:00
Customizing the way models are displayed
Now, we will take a look at how to customize the admin site. Edit the admin.py file of your blog application and change it, as follows:
from django.contrib import admin
from .models import Post
@admin.register(Post)
class PostAdmin(admin.ModelAdmin):
list_display = ('title', 'slug', 'author', 'publish',
'status')
We are telling the Django admin site that our model is registered in the admin site using a custom class that inherits from ModelAdmin. In this class, we can include information about how to display the model in the admin site and how to interact with it. The list_display attribute allows you to set the fields of your model that you want to display in the admin object list page. The @admin.register() decorator performs the same function as the admin.site.register() function we have replaced, registering the ModelAdmin class that it decorates.
Let's customize the admin model with some more options, using the following code:
@admin.register(Post)
class PostAdmin(admin.ModelAdmin):
list_display = ('title', 'slug', 'author', 'publish',
'status')
list_filter = ('status', 'created', 'publish', 'author')
search_fields = ('title', 'body')
prepopulated_fields = {'slug': ('title',)}
raw_id_fields = ('author',)
date_hierarchy = 'publish'
ordering = ('status', 'publish')
Return to your browser and reload the post list page. Now, it will look like this:

You can see that the fields displayed on the post list page are the ones you specified in the list_display attribute. The list page now includes a right sidebar that allows you to filter the results by the fields included in the list_filter attribute. A Search bar has appeared on the page. This is because we have defined a list of searchable fields using the search_fields attribute. Just below the Search bar, there are navigation links to navigate through a date hierarchy: this has been defined by the date_hierarchy attribute. You can also see that the posts are ordered by Status and Publish columns by default. We have specified the default order using the ordering attribute.
Now, click on the Add Post link. You will also note some changes here. As you type the title of a new post, the slug field is filled in automatically. We have told Django to prepopulate the slug field with the input of the title field using the prepopulated_fields attribute. Also, now, the author field is displayed with a lookup widget that can scale much better than a drop-down select input when you have thousands of users, as shown in the following screenshot:

With a few lines of code, we have customized the way our model is displayed on the admin site. There are plenty of ways to customize and extend the Django administration site. You will learn more about this later in this book.
- 6G潛在關(guān)鍵技術(shù)(下冊)
- GPS/GNSS原理與應(yīng)用(第3版)
- Proxmox High Availability
- HCNA網(wǎng)絡(luò)技術(shù)
- SD-WAN架構(gòu)與技術(shù)(第2版)
- 無人機(jī)通信
- 中小型局域網(wǎng)組建、管理與維護(hù)實(shí)戰(zhàn)
- Socket.IO Real-time Web Application Development
- 智慧光網(wǎng)絡(luò):關(guān)鍵技術(shù)、應(yīng)用實(shí)踐和未來演進(jìn)
- 物聯(lián)網(wǎng)長距離無線通信技術(shù)應(yīng)用與開發(fā)
- 通信原理及MATLAB/Simulink仿真
- 計(jì)算機(jī)網(wǎng)絡(luò)技術(shù)及應(yīng)用
- 網(wǎng)管工具使用與技巧大全
- Echo Quick Start Guide
- 一本書讀懂物聯(lián)網(wǎng)