python 尝试内联模型时,<class> 在 Django 中没有 <class> 的外键

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/609556/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 20:28:18  来源:igfitidea点击:

<class> has no foreign key to <class> in Django when trying to inline models

pythondjangodjango-admin

提问by f4nt

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

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

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

我有3种型号:QuizzesQuestions,和Answers

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

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

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.

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

Here's what I have currently:

这是我目前所拥有的:

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'>

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

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

回答by Carl Meyer

You can't do "nested" inlinesin 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).

您不能在 Django 管理员中进行“嵌套”内联(即您不能有内联问题的测验,每个内联问题都有内联答案)。因此,您需要将目光投向仅包含内嵌问题(然后,如果您导航以查看单个问题,它可能会有内嵌答案)。

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]

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.

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

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.

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

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.

另请注意,“extra = 20”意味着每次加载时,每个测验的底部都会有 20 个空白问题表(即使它已经有 20 个实际问题)。也许这就是您想要的 - 使页面很长,但可以轻松地一次添加大量问题。

回答by S.Lott

Let's follow through step by step.

让我们一步一步来。

The error: "Answer has no FK to Quiz".

错误:“Answer has no FK to Quiz”。

That's correct. The Answer model has no FK to Quiz. It has an FK to Question, but not Quiz.

没错。答案模型对测验没有 FK。它有一个 FK 来提问,但没有测验。

Why does Answer need an FK to quiz?

为什么 Answer 需要 FK 来测验?

The QuizAdmin has an AnswerInline and a QuestionInline. For an admin to have inlines, it means the inlined models (Answer and Question) must have FK's to the parent admin.

QuizAdmin 有一个 AnswerInline 和一个 QuestionInline。对于具有内联的管理员,这意味着内联模型(答案和问题)必须具有对父管理员的 FK。

Let's check. Question has an FK to Quiz.

让我们检查。问题有一个 FK 到测验。

And. Answer has no FK to Quiz. So your Quiz admin demands an FK that your model lacks. That's the error.

和。答案对测验没有 FK。因此,您的测验管理员需要您的模型缺少的 FK。这就是错误。

回答by Van Gale

Correct: trying to pull too much out of admin app :) Inline models need a foreign key to the parent model.

正确:试图从管理应用程序中提取太多内容 :) 内联模型需要父模型的外键。