我如何定义Django模型之间的关系多态性? [英] How can I define a polymorphic relation between models in Django?

查看:105
本文介绍了我如何定义Django模型之间的关系多态性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的工作,其中包含一个优惠模型Django应用程序。一个优惠实例包含定价条件,并指向一个产品定义。产品型号实际上是一个层次(我有一个电视模式,摄像机模型等)。所以我想在优惠模式,包含多态(或通用)的关联指向任何产品。

I am working on a Django application which contains an Offer model. An Offer instance contains the pricing conditions and points to a product definition. The product model is actually a hierarchy (I have a Television model, a Camcorder model, etc.). So I would like the Offer model to contain a polymorphic (or "generic") association to point to any product.

目前,所有我发现这是使用通用协会在 CONTENTTYPES 应用程序。这可能会做,但是,我寻找替代品(如有)。

For now, all I have found is this to use the generic associations in the ContentTypes application. This might do, but I am looking for alternatives, if any.

感谢您的帮助。

(每答案,请一个解决方案)

(one solution per answer please)

推荐答案

CONTENTTYPES是正确的做法。
这是因为,所以你需要通过一个中间表,并根据不同的类型做你的分裂ForignKey只能指向一个类型表的。

ContentTypes is the right approach. This is because the ForignKey can only point to one type of table so you need to pass through the an intermediate table and do your split depending on the different type.

因此​​,对于类层次结构模型继承,但为的Con​​tentType外键。

So model inheritance for the class hierarchy, but ContentType for the foreign keys.

这篇关于我如何定义Django模型之间的关系多态性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆