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 支持这个吗?如果支持的话,这样的事情会导致可扩展性问题吗?
谢谢!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,这是支持的。是的,它可能会导致性能问题。您应该阅读 Jacob 关于模型继承的文章: http://jacobian.org/writing/concrete-inheritance/< /a>
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/
它是受支持的,并且不会导致可扩展性问题。然而,我的建议是您只引用子类(即不要创建对基类的引用,也不要实例化它们)。
基本模型类应该是仅扩展的(有点像其他语言中的抽象类)。
It is supported, and won't cause scalability problems. My advice, however, is that you only refer to the Child classes (i.e. don't create references to the Base classes, and don't instantiate them).
Base Model Classes should be extend-only (sort of like an Abstract Class in other languages).