<bdo id='RgJfu'></bdo><ul id='RgJfu'></ul>

      <legend id='RgJfu'><style id='RgJfu'><dir id='RgJfu'><q id='RgJfu'></q></dir></style></legend><tfoot id='RgJfu'></tfoot>

      <small id='RgJfu'></small><noframes id='RgJfu'>

      <i id='RgJfu'><tr id='RgJfu'><dt id='RgJfu'><q id='RgJfu'><span id='RgJfu'><b id='RgJfu'><form id='RgJfu'><ins id='RgJfu'></ins><ul id='RgJfu'></ul><sub id='RgJfu'></sub></form><legend id='RgJfu'></legend><bdo id='RgJfu'><pre id='RgJfu'><center id='RgJfu'></center></pre></bdo></b><th id='RgJfu'></th></span></q></dt></tr></i><div id='RgJfu'><tfoot id='RgJfu'></tfoot><dl id='RgJfu'><fieldset id='RgJfu'></fieldset></dl></div>

      1. &lt;类&gt;没有 &lt;class&gt; 的外键在 Django 中尝试内联模

        时间:2023-11-07

          <bdo id='LjUFC'></bdo><ul id='LjUFC'></ul>

          <small id='LjUFC'></small><noframes id='LjUFC'>

          <i id='LjUFC'><tr id='LjUFC'><dt id='LjUFC'><q id='LjUFC'><span id='LjUFC'><b id='LjUFC'><form id='LjUFC'><ins id='LjUFC'></ins><ul id='LjUFC'></ul><sub id='LjUFC'></sub></form><legend id='LjUFC'></legend><bdo id='LjUFC'><pre id='LjUFC'><center id='LjUFC'></center></pre></bdo></b><th id='LjUFC'></th></span></q></dt></tr></i><div id='LjUFC'><tfoot id='LjUFC'></tfoot><dl id='LjUFC'><fieldset id='LjUFC'></fieldset></dl></div>
              <tbody id='LjUFC'></tbody>
                <legend id='LjUFC'><style id='LjUFC'><dir id='LjUFC'><q id='LjUFC'></q></dir></style></legend>

                  <tfoot id='LjUFC'></tfoot>
                1. 本文介绍了&lt;类&gt;没有 &lt;class&gt; 的外键在 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 中尝试内联模型时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:Django 管理模型继承是否可能? 下一篇:如何在 Django Admin 中仅折叠一个字段?

                  相关文章

                      <bdo id='AVBGc'></bdo><ul id='AVBGc'></ul>
                    <i id='AVBGc'><tr id='AVBGc'><dt id='AVBGc'><q id='AVBGc'><span id='AVBGc'><b id='AVBGc'><form id='AVBGc'><ins id='AVBGc'></ins><ul id='AVBGc'></ul><sub id='AVBGc'></sub></form><legend id='AVBGc'></legend><bdo id='AVBGc'><pre id='AVBGc'><center id='AVBGc'></center></pre></bdo></b><th id='AVBGc'></th></span></q></dt></tr></i><div id='AVBGc'><tfoot id='AVBGc'></tfoot><dl id='AVBGc'><fieldset id='AVBGc'></fieldset></dl></div>

                      <small id='AVBGc'></small><noframes id='AVBGc'>

                    1. <tfoot id='AVBGc'></tfoot><legend id='AVBGc'><style id='AVBGc'><dir id='AVBGc'><q id='AVBGc'></q></dir></style></legend>