Django模型继承与关系

编程入门 行业动态 更新时间:2024-10-21 18:49:01
本文介绍了Django模型继承与关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个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是否支持此功能?如果被支持,这样会导致可扩展性问题?

谢谢!

解决方案

是的,这是支持的。是的,可能会导致性能问题。您应该阅读Jacob关于模型继承的文章: jacobian/writing/concrete-inheritance/

自1.0以来,Django支持的模型继承。这是一个整洁的功能,可以在您的建模选项中增加的灵活性。可以在

然而,模型继承还提供a真的很好的机会拍自己的脚:具体(多表)继承。如果你使用具体的继承,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!

解决方案

Yes, this is supported. Yes, it can cause performance problems. You should read Jacob's post on model inheritance: jacobian/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.

更多推荐

Django模型继承与关系

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

发布评论

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

>www.elefans.com

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