Django模型的继承与关系(Django Model Inheritance and Relationships)

编程入门 行业动态 更新时间:2024-10-27 15:16:16
Django模型的继承与关系(Django Model Inheritance and Relationships)

我有一个django应用程序,我想在基础级别定义两个类之间的关系。 定义这些基类的子项之间的关系对我来说也是有意义的 - 所以我得到这样的东西:

class BaseSummary(models.Model): base_types... class BaseDetail(models.Model): base_detail_types... base_summary = models.ForeignKey('BaseSummary') class ChildSummary(BaseSummary): child_summary_types... class ChildDetail(BaseDetail): child_detail_type... child_summary = models.ForeignKey('ChildSummary')

django支持这个吗? 如果它受支持,这样的事情会导致可扩展性问题吗?

谢谢!

I've got a django app, where I'd like to define a relationship between two classes at a base level. It also makes sense to me to define the relationship between the children of those base classes - so that I get something like this:

class BaseSummary(models.Model): base_types... class BaseDetail(models.Model): base_detail_types... base_summary = models.ForeignKey('BaseSummary') class ChildSummary(BaseSummary): child_summary_types... class ChildDetail(BaseDetail): child_detail_type... child_summary = models.ForeignKey('ChildSummary')

Does django support this? and If it is supported, is something like this going to cause scalability problems?

Thanks!

最满意答案

是的,这是支持的。 是的,它可能会导致性能问题。 你应该阅读Jacob关于模型继承的帖子: http : //jacobian.org/writing/concrete-inheritance/

从1.0开始,Django支持模型继承。 这是一个简洁的功能,可以大大提高您的建模选项的灵活性。

然而,模型继承也提供了一个非常好的机会射击自己的脚:具体(多表)继承。 如果您正在使用具体的继承,Django几乎在每个查询上都会创建隐式连接回父表。 这可能会彻底破坏数据库的性能。

Yes, this is supported. Yes, it can cause performance problems. You should read Jacob's post on model inheritance: http://jacobian.org/writing/concrete-inheritance/

Since 1.0, Django’s supported model inheritance. It’s a neat feature, and can go a long way towards increasing flexibility in your modeling options.

However, model inheritance also offers a really excellent opportunity to shoot yourself in the foot: concrete (multi-table) inheritance. If you’re using concrete inheritance, Django creates implicit joins back to the parent table on nearly every query. This can completely devastate your database’s performance.

更多推荐

本文发布于:2023-07-19 17:18:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1183978.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:模型   关系   Django   Relationships   Inheritance

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!