1. 列表显示数据模型的字段
polls/models.py
from django.db import models
from django.utils.encoding import python_2_unicode_compatible
@python_2_unicode_compatible # only if you need to support Python 2
class Question(models.Model):
# ...
def__str__(self):
return self.question_text
@python_2_unicode_compatible # only if you need to support Python 2
class Choice(models.Model):
# ...
def__str__(self):
return self.choice_text
2. 显示多列数据
polls/admin.py
class QuestionAdmin(admin.ModelAdmin):
# ...
list_display=('question_text','pub_date')
3. 调整数据项的顺序
polls/admin.py
from django.contrib import admin
from.models import Question
classQuestionAdmin(admin.ModelAdmin):
fields=['pub_date','question_text']
admin.site.register(Question,QuestionAdmin)
4. 数据项分组(Group)
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)
5. 列表显示多个数据项目
polls/admin.py
class QuestionAdmin(admin.ModelAdmin):
# ...
list_display=('question_text','pub_date')