对于与 FK 没有正式相关的模型,django 模型上的强制关系
我试图弄清楚当没有从子模型到父模型的直接 FK 时,如何让 django 管理系统将我的模型显示为内联。
我有三个模型(伪代码):
class CampaignMain(models.model):
...
class CampaignMonitor(models.model):
campaign = models.OneToOneField(CampaignMain, pk=True)
class CampaignTransaction(models.model):
campaign = models.ForeignKey(CampaignMain)
CampaignMonitor 和 CampaignTransaction FK CampaignMain,这就是我需要的结构方式。
这是我无法理解的一点:我需要一个管理页面,其中显示 CampaignMonitor 和 CampaignTransaction 作为内联。但是当我尝试这个时,我得到“CampaignTransaction 中没有指向 CampaignMonitor 的错误”
有没有办法“强制”管理页面的关系?或者有通用的 FK 选项吗?我在 contrib/contenttypes 中看到了一些东西,但它似乎不是我需要的。或者我是否必须以这种方式为两个模型构建自定义管理部分?
一如既往,非常感谢您的建议。
伊曼克
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.
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.
Here's the bit I can't fathom: I need an admin page showing CampaignMonitor with CampaignTransaction as inlines. But when I try this, I get "error no fk in CampaignTransaction pointing to CampaignMonitor"
Is there a way to "force" the relationship just for the admin page? Or is there a generic FK option? I saw something in contrib/contenttypes, but it doesn't seem to be what I need. 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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用 多表继承,它使用一对一关系实现:
现在根据您的需要修改 CampaignMonitor 的管理。
Instead of OneToOneField you can use Multi-table inheritance, which implemented using a one-to-one relationshinp:
Now modify CampaignMonitor's admin as needed for your needs.