<类>没有< class>的外键在Django尝试内嵌模型 [英] <class> has no foreign key to <class> in Django when trying to inline models

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

问题描述

我需要能够创建一个具有20个奇怪的选择题的测验类型的应用程序。

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

我有3个模型:测验问题答案

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?

推荐答案

你不能做嵌套内联在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,因为答案没有外键的测验。

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尝试内嵌模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆