本文介绍了<类>没有 <class> 的外键在 Django 中尝试内联模型时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要能够创建一个包含 20 个奇怪的多项选择题的测验类型的应用程序.

I need to be able to create a quiz type application with 20 some odd multiple choice questions.

我有 3 个模型:QuizzesQuestionsAnswers.

I have 3 models: Quizzes, Questions, and Answers.

我想在管理界面中创建一个测验,并内联测验和答案元素.

I want in the admin interface to create a quiz, and inline the quiz and answer elements.

目标是单击添加测验",然后转到包含 20 个问题字段的页面,每个字段有 4 个答案字段.

The goal is to click "Add Quiz", and be transferred to a page with 20 question fields, with 4 answer fields per each in place.

这是我目前拥有的:

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)

我尝试添加测验时收到以下错误:

I get the following error when I try to add a quiz:

class 'quizzer.quiz.models.Answer'> has no ForeignKey to <class 'quizzer.quiz.models.Quiz'>

这是可行的,还是我试图从 Django Admin 应用程序中提取太多内容?

Is this doable, or am I trying to pull too much out of the Django Admin app?

推荐答案

你不能这样做 "nested"inlines 在 Django 管理员中(即你不能有一个带有内联问题的测验,每个内联问题都有内联答案).因此,您需要将目光放低到仅具有内联问题(然后,如果您导航以查看单个问题,它可能具有内联答案).

You can't do "nested" inlines in the Django admin (i.e. you can't have a Quiz with inline Questions, with each inline Question having inline Answers). So you'll need to lower your sights to just having inline Questions (then if you navigate to view a single Question, it could have inline Answers).

所以你的模型很好,但你的管理代码应该是这样的:

So your models are fine, but your admin code should look like this:

class QuestionInline(admin.TabularInline):
    model = Question
    extra = 20

class AnswerInline(admin.TabularInline):
    model = Answer
    extra = 4

class QuestionAdmin(admin.ModelAdmin):
    inlines = [AnswerInline]

class AnswerAdmin(admin.ModelAdmin):
    pass

class QuizAdmin(admin.ModelAdmin):
    inlines = [QuestionInline]

AnswerAdmin 拥有 AnswerInline 或 QuestionAdmin 拥有 QuestionInline 没有意义(除非这些模型具有自引用外键).而且 QuizAdmin 不能有 AnswerInline,因为 Answer 没有 Quiz 的外键.

It doesn't make sense for AnswerAdmin to have an AnswerInline, or QuestionAdmin to have a QuestionInline (unless these were models with a self-referential foreign key). And QuizAdmin can't have an AnswerInline, because Answer has no foreign key to Quiz.

如果 Django 确实支持嵌套内联,则 QuestionInline 的逻辑语法是接受您设置为 [AnswerInline] 的内联"属性.但事实并非如此.

If Django did support nested inlines, the logical syntax would be for QuestionInline to accept an "inlines" attribute, which you'd set to [AnswerInline]. But it doesn't.

另请注意,extra = 20"意味着每次加载时,每个测验的底部都会有 20 个空白问题表格(即使它已经有 20 个实际问题).也许这就是您想要的 - 页面很长,但同时添加很多问题也很容易.

Also note that "extra = 20" means you'll have 20 blank Question forms at the bottom of every Quiz, every time you load it up (even if it already has 20 actual Questions). Maybe that's what you want - makes for a long page, but makes it easy to add lots of questions at once.

这篇关于&lt;类&gt;没有 &lt;class&gt; 的外键在 Django 中尝试内联模型时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 23:47