Django 1.10中文文档:第一个应用 part 7

已经同步到gitbook,想阅读的请转到gitbook: Django 1.10 中文文档

Writing your first Django app, part 7¶

This tutorial begins where Tutorial 6 left off. We’re continuing the Web-poll application and will focus on customizing Django’s automatically-generated admin site that we first explored in Tutorial 2.

紧接着Tutorial 6。我们继续开发投票应用并主要关注Django自动生成的admin站点,这部分我们在教程2中已经了解过。

Customize the admin form¶

By registering the Question model with admin.site.register(Question), Django was able to construct a default form representation. Often, you’ll want to customize how the admin form looks and works. You’ll do this by telling Django the options you want when you register the object.

通过admin.site.register(Question)语句将Question模型注册到admin,Django会自动生成该模型的默认表单。一般情况下,你需要定制表单的样式和功能。这样在你注册模型的时候就需要告诉Django你想要的选项。

Let’s see how this works by reordering the fields on the edit form. Replace the admin.site.register(Question)
line with:

我们通过演示对表单字段的重新排序,来了解其工作原理。用以下代码替换admin.site.register(Question)语句:

polls/admin.py

from django.contrib import admin

from .models import Question


class QuestionAdmin(admin.ModelAdmin):
    fields = ['pub_date', 'question_text']

admin.site.register(Question, QuestionAdmin)

You’ll follow this pattern – create a model admin class, then pass it as the second argument to admin.site.register() – any time you need to change the admin options for a model.

一般遵循以下步骤——创建一个model admin类,然后作为admin.site.register()的第二个参数——你可以随时修改模型的admin选项。

This particular change above makes the “Publication date” come before the “Question” field:

这个操作将使“Publication date” 放在“Question” 字段之前:

Django 1.10中文文档:第一个应用 part 7_第1张图片
Paste_Image.png

This isn’t impressive with only two fields, but for admin forms with dozens of fields, choosing an intuitive order is an important usability detail.

只有两个字段的情况下,感觉没什么作用,但是对于有几十个字段的表单来说,采用更直观的排序方式将是一个值得引起注意的细节问题。

And speaking of forms with dozens of fields, you might want to split the form up into fieldsets:

而且对于有几十个字段的表单来说,你也许想要通过fieldsets来分隔开:

polls/admin.py

from django.contrib import admin

from .models import Question


class QuestionAdmin(admin.ModelAdmin):
    fieldsets = [
        (None,               {'fields': ['question_text']}),
        ('Date information', {'fields': ['pub_date']}),
    ]

admin.site.register(Question, QuestionAdmin)

The first element of each tuple in fieldsets is the title of the fieldset. Here’s what our form looks like now:

fieldsets 内元组的第一个字段是fieldsets的标题。我们的表单看起来应该像以下这样了:

Django 1.10中文文档:第一个应用 part 7_第2张图片
Paste_Image.png

Adding related objects¶

OK, we have our Question admin page, but a Question
has multiple Choices, and the admin page doesn’t display choices.

好的,我们已经有Question的admin页面了,但是一个Question有很多个Choices,admin页面也没有展示这些choices。

Yet. There are two ways to solve this problem. The first is to register Choice with the admin just as we did with Question. That’s easy:

但是,我们有两种方法来解决这个问题。第一种方法是像Question一样将Choice注册到admin站点,这非常简单:

polls/admin.py

from django.contrib import admin

from .models import Choice, Question
# ...
admin.site.register(Choice)

Now “Choices” is an available option in the Django admin. The “Add choice” form looks like this:

现在“Choices”已经展示在Django admin站点了,“增加choice”表单看起来如下图:

Django 1.10中文文档:第一个应用 part 7_第3张图片
Paste_Image.png

In that form, the “Question” field is a select box containing every question in the database. Django knows that a ForeignKey should be represented in the admin as a