Django 简明教程

Django Admin – Set Fields to Display

当一个模型注册到 Django 的管理站点时,当您点击该模型的名称时,它对象列表会显示在列表中。

When a model is registered to Django’s Admin site, its list of objects is displayed when you click the name of the model.

在下图中, Employees 模型中的对象列表会显示在列表中−

In the following figure, the list of objects in the Employees model is displayed −

django admin set fields 1

然而,上述页面以 Employee object(1) 的表格显示对象,这不会显示 namecontact 等属性。为了让对象描述更加用户友好,我们需要在 employee model class 中覆盖 str() method

However, the above page shows the objects in the form Employee object(1), which doesn’t reveal the attributes such as the name or contact, etc. To make the object description more user-friendly, we need to override the str() method in the employee model class.

employee class 被重写如下,以给出其对象的替代字符串表示。

The employee class is rewritten as below to give the alternate string representation of its object.

from django.db import models

class Employee(models.Model):
   empno = models.CharField(max_length=20)
   empname = models.CharField(max_length=100)
   contact = models.CharField(max_length=15)
   salary = models.IntegerField()
   joined_date = models.DateField(null=True)

   class Meta:
      db_table = "employee"

   def __str__(self):
      return "Name: {}, Contact: {}".format(self.empname, self.contact)

对模型定义进行这些更改,并刷新管理站点的首页(如果 Django 服务器尚未运行,则启动)。

Make these changes to the model definition, and refresh the admin site’s homepage (Start the Django server if it isn’t already running).

在 MYAPP 中点击“员工”模型以显示 employee object 详细信息 −

Click the Employees model under MYAPP to display the employee object details −

django admin set fields 2

如果你点击任一对象详细信息,会出现显示对象属性的窗体,你可以在此处更新对象或删除对象。

If you click any of the object details, a form that shows the object attributes appears, and you can update the object or delete the object from here.

django admin set fields 3

你可以使用 admin.ModelAdmin 对象的“ list_display ”属性来自定义“更改列表页面”,该属性指定在更改列表中显示的列。

You can customize the "change list page" with the "list_display" attribute of an admin.ModelAdmin object that specifies what columns are shown in the change list.

ModelAdmin class 是模型在管理员界面中的表现形式。通常,这些存储在应用程序中名为 admin.py 的文件中。

The ModelAdmin class is the representation of a model in the admin interface. Usually, these are stored in a file called admin.py in your application.

通过继承 ModelAdmin class 来定义 EmployeeAdmin class 。将 list_display 属性设置为要显示的字段的元组。

Define the EmployeeAdmin class by inheriting from the ModelAdmin class. Set the list_display attribute as a tuple of the fields to be displayed.

from django.contrib import admin

# Register your models here.
from .models import Employee

class EmployeeAdmin(admin.ModelAdmin):
   list_display = ("empname", "contact", "joined_date")

按如下所示注册 EmployeeAdmin 类 −

Register the EmployeeAdmin class as follows −

admin.site.register(Employee, EmployeeAdmin)

你也可以使用 @admin.register() decorator 注册此模型 −

You can also register the model using the @admin.register() decorator

from django.contrib import admin

# Register your models here.
from .models import Employee

@admin.register(Employee)
class EmployeeAdmin(admin.ModelAdmin):
   list_display = ("empname", "contact", "joined_date")

保存以上更改并刷新管理员站点以显示员工对象列表 −

Save the above changes and refresh the Admin site to display the list of Employee objects −

django admin set fields 4

你还可以向列表页面提供搜索功能。在 EmployeeAdmin 类中添加 search_fields 属性 −

You can also provide a searching facility to the list page. Add search_fields attribute in the EmployeeAdmin class −

@admin.register(Employee)
class EmployeeAdmin(admin.ModelAdmin):
   list_display = ("empname", "contact", "joined_date")
   search_fields = ("empname__startswith", )

现在,列表页面在顶部显示搜索字段。搜索基于员工姓名进行。 __startswith 修饰符将搜索限制为以搜索参数开头的名称。

Now the list page shows a search field on the top. The search is based on employee’s name. The __startswith modifier restricts the search to names that begin with the search parameter.

django admin set fields 5

因此,Django 管理员界面是完全可定制的。你可以通过在管理员类中提供字段属性来设置在更新窗体中要提供的字段。

Thus, the Django Admin interface is completely customizable. You can set the fields to be provided in the update form by providing the fields attribute in the admin class.