问题描述
我有具有 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 之外的其他东西(例如我们认为正确的做法是在 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)
现在,当我在管理视图中查看一个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的方向性,所以我不能做内联。上面的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 ,而这又是属于这个数据文件。
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.
我阅读了,但解决方法是扭转关系的方向。我不认为我可以这样做,因为其他对象(套件)也有 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.
我的问题是:为了在内部显示LogOutput 需要做什么DataFile的管理页面? (或者我是否想使用需要修改的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和管理员非常聪明,但是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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!