对于与 FK 没有正式相关的模型,django 模型上的强制关系

发布于 2024-10-31 05:43:04 字数 695 浏览 1 评论 0原文

我试图弄清楚当没有从子模型到父模型的直接 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

简美 2024-11-07 05:43:04

您可以使用 多表继承,它使用一对一关系实现:

class CampaignMonitor(CampaignMain): 
    ...

现在根据您的需要修改 CampaignMonitor 的管理。

Instead of OneToOneField you can use Multi-table inheritance, which implemented using a one-to-one relationshinp:

class CampaignMonitor(CampaignMain): 
    ...

Now modify CampaignMonitor's admin as needed for your needs.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文