Django模型继承与关系 [英] Django Model Inheritance and Relationships

查看:181
本文介绍了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关于模型继承的文章: http://jacobian.org/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: 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.

这篇关于Django模型继承与关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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