Use OneToOneField inlined in Django Admin(在 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 中使用内联 OneToOneField
基础教程推荐
- 合并具有多索引的两个数据帧 2022-01-01
- 如何在Python中绘制多元函数? 2022-01-01
- 使用Python匹配Stata加权xtil命令的确定方法? 2022-01-01
- 如何在 Python 中检测文件是否为二进制(非文本)文 2022-01-01
- 将 YAML 文件转换为 python dict 2022-01-01
- 使 Python 脚本在 Windows 上运行而不指定“.py";延期 2022-01-01
- 哪些 Python 包提供独立的事件系统? 2022-01-01
- Python 的 List 是如何实现的? 2022-01-01
- 使用 Google App Engine (Python) 将文件上传到 Google Cloud Storage 2022-01-01
- 症状类型错误:无法确定关系的真值 2022-01-01