django搭建web (三) admin.py -- 待续
demo
关于模型myQuestion,myAnswer将在后述博客提及
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.contrib import admin
from .models import myQuestion,myAnswer
# Register your models here.
class answerInLine(admin.TabularInline):
model = myAnswer
extra = 4
class myQuestionAdmin(admin.ModelAdmin):
inlines = [answerInLine]
#fieldsets = [
# (None, {'fields': ['question_text']}),
# ('Date information', {'fields': ['pub_date'], 'classes': ['collapse']}),
#]
#fieldsets = ['question_text','answer_question']
#list_display = ('question_text', 'pub_date')
admin.site.register(myQuestion,myQuestionAdmin)
admin.site.register(myAnswer)
1. 从models目录下引入模型myQuestion和myAnswer
2. 在answerInLine函数中如果传入参数 admin.TabularInline 则会出现以下效果
其中 model被赋值为模型myAnswer 因为在模型myAnswer中 question = models.ForeignKey(myQuestion) 多个答案共用一个问题,设置为外键形式
extra = 4 默认设置4个该model
如果将TabularInline换成StackedInline 则会出现以下效果