我有一个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模型继承与关系
发布评论