1

我在 Django 中有以下模型(为便于阅读而简化):

class Test(models.Model):
    pass

class Page(models.Model):
    test = models.ForeignKey(Test)

class Question(models.Model);
    page = models.ForeignKey(Page)

class Answer(models.Model):
    question = models.ForeignKey(Question)

我想在管理区域完全编辑一个测试,即编辑Test数据,Page为该测试添加Questions,然后为每个页面添加 s,Answer为每个问题添加 s。这样做的最简单和 Django 模式是什么?

4

1 回答 1

1
from django.contrib import admin
from project.apps.models import Test, Page,Question, Answer

class AnswerInline(admin.StackedInline):
model = Answer
extra = 0

class QuestionInline(admin.StackedInline):
model = Question
extra = 0

class PageInline(admin.StackedInline):
model = Page
extra = 0

class TestAdmin(admin.ModelAdmin):
list_display =('__unicode__','item1','item2')
search_fields = ('field1','field2')
inlines=[AnswersItemInline,QuestionsItemInline,PageItemInline]

admin.site.register(Test,TestAdmin)
于 2013-10-21T13:15:32.380 回答