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

      1. <small id='Sb1NT'></small><noframes id='Sb1NT'>

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

        在 Django Admin 中使用内联 OneToOneField

        时间:2023-11-07
        • <small id='TFmCd'></small><noframes id='TFmCd'>

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

            <legend id='TFmCd'><style id='TFmCd'><dir id='TFmCd'><q id='TFmCd'></q></dir></style></legend>
              • <bdo id='TFmCd'></bdo><ul id='TFmCd'></ul>

                • <tfoot id='TFmCd'></tfoot>

                  本文介绍了在 Django Admin 中使用内联 OneToOneField的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有 DataFile 模型,其中有 LogOutput 对象.DataFile 对象将有一个 LogOutput.对于属于 DataFile 的任何 LogOutput,它将只属于单个 DataFile.其他模型也有 LogOutput 对象.

                  I have DataFile models which have LogOutput objects. A DataFile object will have a single LogOutput. For any LogOutput that belongs to a DataFile, it will only belong to a single DataFile. Other models also have LogOutput objects.

                  因为它们是一对一的,除了 LogOutputs 可以属于 DataFiles 以外的东西(例如,Suites 也有它们-- 请参见下面的代码)我认为正确的做法是在 DataFile 中定义一个 OneToOneField,即 LogOutput.

                  Since they are one-to-one, except that LogOutputs can belong to things other than DataFiles (eg. Suites have them too -- see code below) I thought the right thing to do would be to have a OneToOneField defined in DataFile that is the LogOutput.

                  models.py:

                  class LogOutput(models.Model):
                      raw = models.TextField()
                  
                  class DataFile(models.Model):
                      name = models.CharField()#etc.
                      logoutput = models.OneToOneField(LogOutput)
                  
                  class Suite(models.Model):
                      # Many Suites will have the same datafile:
                      datafile = models.ForeignKey(DataFile)
                  
                      # Each Suite has a unique LogOutput different from the DataFile's
                      # and as with the DataFile, that LogOutput will have just one Suite
                      logoutput = models.OneToOneField(LogOutput)
                  

                  现在,当我在 Admin 视图中查看 DataFile 时,我想查看 LogOutput,所以我想我会内联它.

                  Now, when I look at a DataFile in the Admin view, I want to see the LogOutput, so I thought I would just inline it.

                  admin.py:

                  class LogOutputInline(admin.TabularInline):
                      model = LogOutput
                  
                  class DataFileAdmin(admin.ModelAdmin):
                      search_fields = ['name']
                      inlines = [LogOutputInline]
                  
                  admin.site.register(DataFile, DataFileAdmin)
                  

                  似乎由于 OneToOneField(s) 定义位置的方向性,我无法进行内联.上面的 admin.py 给了我:

                  It appears that because of the directionality of where the OneToOneField(s) are defined, I can't do the inlining. The above admin.py gives me:

                  <class 'trial.models.LogOutput'> has no ForeignKey to <class 'trial.models.DataFile'>
                  

                  这当然是对的,但我看不出它是如何相关的,因为 DataFile 有一个(并且只有一个)LogOutput 反过来,只属于这一个DataFile.

                  Which, of course is true, but I don't see how it's relevant, because a DataFile has one (and only one) LogOutput which, in turn, belongs to only this one DataFile.

                  我阅读了 问题 1744203 但解决方案是逆转关系的方向.我认为我不能这样做,因为其他对象(Suites)也有 LogOutputs.

                  I read Question 1744203 but the solution there was to reverse the direction of the relationship. I don't think I can do that because other objects (Suites) also have LogOutputs.

                  而且,如果重要的话,这是在 Django 1.5 中.

                  And, if it matters, this is in Django 1.5.

                  我的问题是:我需要做什么才能在 DataFile 的 管理页面上显示内联 LogOutput?(或者我是否考虑使用 OneToOneField 需要修改?)

                  My question is: What do I need to do in order to display the inline LogOutput on the DataFile's admin page? (Or is my thinking about the use of a OneToOneField in need of revision?)

                  TIA!

                  推荐答案

                  内部 ORM 和 admin 非常聪明,但是在 OOP 遇到表结构时,事情可能有点不精确.我建议使用 抽象基类像这样:

                  The internal ORM and admin are pretty smart, but where OOP runs up against table structure, things can be a bit imprecise. I'd recommend giving the ORM a little hint by using an abstract base class like this:

                  class LogOutput(models.Model):
                      raw = models.TextField()
                  
                      class Meta:
                          abstract = True
                  
                  class DataFileLogOutput(LogOutput):
                      pass
                  
                  class SuiteFileLogOutput(LogOutput):
                      pass
                  
                  class DataFile(models.Model):
                      name = models.CharField()#etc.
                      logoutput = models.OneToOneField(DataFileLogOutput)
                  
                  class Suite(models.Model):
                      # Many Suites will have the same datafile:
                      datafile = models.ForeignKey(DataFile)
                  
                      # Each Suite has a unique LogOutput different from the DataFile's
                      # and as with the DataFile, that LogOutput will have just one Suite
                      logoutput = models.OneToOneField(SuiteLogOutput)
                  

                  这篇关于在 Django Admin 中使用内联 OneToOneField的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:在 Django Admin 中订购多对多字段 下一篇:在admin django 1.6中注册抽象模型

                  相关文章

                • <small id='LvSPM'></small><noframes id='LvSPM'>

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

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