我需要能够创建一个包含 20 个奇怪的多项选择题的测验类型的应用程序。
我有 3 个模型:Quizzes
、Questions
和Answers
。
我想在管理界面中创建一个测验,并内联测验和答案元素。
目标是单击“添加测验”,然后转到包含 20 个问题字段的页面,每个字段有 4 个答案字段。
这是我目前拥有的:
class Quiz(models.Model):
label = models.CharField(blank=true, max_length=50)
class Question(models.Model):
label = models.CharField(blank=true, max_length=50)
quiz = models.ForeignKey(Quiz)
class Answer(models.Model):
label = models.CharField(blank=true, max_length=50)
question = models.ForeignKey(Question)
class QuestionInline(admin.TabularInline):
model = Question
extra = 20
class QuestionAdmin(admin.ModelAdmin):
inlines = [QuestionInline]
class AnswerInline(admin.TabularInline):
model = Answer
extra = 4
class AnswerAdmin(admin.ModelAdmin):
inlines = [AnswerInline]
class QuizAdmin(admin.ModelAdmin):
inlines = [QuestionInline, AnswerInline]
admin.site.register(Question, QuestionAdmin)
admin.site.register(Answer, AnswerAdmin)
admin.site.register(Quiz, QuizAdmin)
尝试添加测验时出现以下错误:
class 'quizzer.quiz.models.Answer'> has no ForeignKey to <class 'quizzer.quiz.models.Quiz'>
这是可行的,还是我试图从 Django Admin 应用程序中提取太多内容?