官方文档#7-定制管理台

2017-09-15  本文已影响0人  wangfp

1. 管理台表单布局(1)

# 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)
image

2. 管理台表单布局(2)

# polls/admin.py
from django.contrib import admin

from .models import Question


class QuestionAdmin(admin.ModelAdmin):
    # 将表单内容分成两块(第一块为命名,第二块命名为'Date information')
    # 每一块保存在一个tuple中
    fieldsets = [
        (None,               {'fields': ['question_text']}),
        ('Date information', {'fields': ['pub_date']}),
    ]

admin.site.register(Question, QuestionAdmin)
image

3. 添加关联对象

# polls/admin.py
from django.contrib import admin

from .models import Choice, Question


# 除了StackedInline这种内联模式外,Django还提供一种TabularInline的内联模式(每个Choice选项将展示在一行)
class ChoiceInline(admin.StackedInline):
    model = Choice
    # 显示3个额外的Choice表单
    extra = 3


class QuestionAdmin(admin.ModelAdmin):
    fieldsets = [
        (None,               {'fields': ['question_text']}),
        ('Date information', {'fields': ['pub_date'], 'classes': ['collapse']}),
    ]
    # 告诉Django该表单有一个内联表单
    inlines = [ChoiceInline]

admin.site.register(Question, QuestionAdmin)
StackedInline
TabularInline

4. 定制管理台的Change List

# polls/admin.py
class QuestionAdmin(admin.ModelAdmin):
    # ...
    list_display = ('question_text', 'pub_date', 'was_published_recently')
change list
上一篇下一篇

猜你喜欢

热点阅读