简体   繁体   English

在django模型上强制建立与正式FK不相关的模型的关系

[英]Force relationships on django models for models that aren't officially FK related

I'm trying to figure out how to get the django admin system to display my models as inlines, when there isn't a direct FK from child to parent model. 我试图弄清楚当没有直接从子模型到父模型的FK时,如何使django管理系统将我的模型显示为内联。

I have three models (pseudo code): 我有三个模型(伪代码):

class CampaignMain(models.model):
    ...

class CampaignMonitor(models.model): 
    campaign = models.OneToOneField(CampaignMain, pk=True)

class CampaignTransaction(models.model):
    campaign = models.ForeignKey(CampaignMain)

So both CampaignMonitor and CampaignTransaction FK CampaignMain, which is the way I need it to be structured. 因此,CampaignMonitor和CampaignTransaction FK CampaignMain都是我需要的结构方式。

Here's the bit I can't fathom: I need an admin page showing CampaignMonitor with CampaignTransaction as inlines. 这是我无法理解的一点:我需要一个管理页面,以CampaignTransaction作为内联显示CampaignMonitor。 But when I try this, I get "error no fk in CampaignTransaction pointing to CampaignMonitor" 但是,当我尝试这样做时,我收到“ CampaignTransaction中没有错误,指向CampaignMonitor的错误”

Is there a way to "force" the relationship just for the admin page? 有没有一种方法可以仅针对管理页面来“强制”这种关系? Or is there a generic FK option? 还是有通用的FK选项? I saw something in contrib/contenttypes, but it doesn't seem to be what I need. 我看到了contrib / contenttypes中的某些内容,但这似乎不是我所需要的。 Or am I going to have to build a custom admin section to two models in that way? 还是我必须以这种方式为两个模型构建自定义管理部分?

As always advice is greatly appreciated. 一如既往的建议非常感谢。

imanc imanc

Instead of OneToOneField you can use Multi-table inheritance , which implemented using a one-to-one relationshinp: 代替OneToOneField,您可以使用Multi-table继承 ,该继承通过一对一的关系实现:

class CampaignMonitor(CampaignMain): 
    ...

Now modify CampaignMonitor's admin as needed for your needs. 现在,根据需要修改CampaignMonitor的管理员。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM