Django Twenty-Three-Custom display of data table data in the admin management background

I. Introduction

The list display content of each data table, by default, only displays the title information of each data in the data table. But if we want to view each table field value of each data specifically, we must click on the header information to enter the next level of data details page to view it, which is not very intuitive.

In [admin.py] in each app, we can customize the list display content of each data table, and we can also add a search box to quickly search for relevant data accurately and fuzzy;

Next, we do the following complete related operations;

 

2. Customize the list display content of a specified data table

detail:

①. We take the data sheet [hello_person] as the experimental data;

1. The first step: If we want all the table field values ​​in the data table [hello_person] to be displayed, we need to add code content such as the list_display attribute to the [admin.py] in the application [hello]. The relevant code is as follows


from django.contrib import admin



# Register your models here.



from hello import models





# 类名可以随意

class ControlPerson(admin.ModelAdmin):

    '''自定义hello_person表在admin管理后台的数据列表展示页面里展示哪几个表字段内容,需要重写属性list_display'''

    list_display = ('id',"name","age")   # 重写属性list_display,来设置展示的表字段

    







admin.site.register(models.Person,ControlPerson)

2. Step 2: Restart the service of the django project [helloworld]

3. Step 3: Re-login to the admin management background successfully

4. Step 4: View the list display content of the data table [hello_person]

detail:

①. The default value of the attribute [list_display] in the parent class [ModelAdmin] is [('__str__',)], so the list display content of the data table by default only displays the title information of each data in the data table;

 

②. The data type of the attribute [list_display] is list, which can contain any number of table field names and the order of the table field names can be arbitrary; for example, there is only one value [id] in the attribute [list_display];

 

③. Table field names that contain lowercase letters will be displayed in uppercase letters on the list page corresponding to the admin management background page;

 Three, add search function

When there is too much data in the data table [hello_person], we can add a search function to find the data we want quickly, and we can use the attribute [search_fields] to add a search bar to the list page:

1. The first step: add a search_fields attribute on the basis of the code just now, set the search conditions to perform precise search and fuzzy search according to the table field name

2. Step 2: Restart the service of the django project [helloworld]

3. Step 3: Re-login to the admin management background successfully

4. Step 4: Check whether there is an extra search box in the list of data table [hello_person] and supports precise search and fuzzy search

 

 detail:

①. The attribute search_fields value supports passing multiple table field names, which means that it supports accurate search and fuzzy search according to any one of the multiple table field names;

     For example, when the attribute search_fields value is equal to [("name", "age")], then it means that precise search and fuzzy search can be performed according to the table field name name or the table field name age;

 

 

 

 

Guess you like

Origin blog.csdn.net/LYX_WIN/article/details/114627775