django模型的无缝多态继承
django-polymorphic-ng的Python项目详细描述
django的多态模型
django多态性简化了django项目中使用继承模型的过程。 在基模型上进行查询时,将返回继承的模型类。
当我们存储从Projectmodel继承的模型时…
>>> Project.objects.create(topic="Department Party") >>> ArtProject.objects.create(topic="Painting with Tim", artist="T. Turner") >>> ResearchProject.objects.create(topic="Swallow Aerodynamics", supervisor="Dr. Winter")
…要检索所有项目,将返回子类模型!
>>> Project.objects.all() [ <Project: id 1, topic "Department Party">, <ArtProject: id 2, topic "Painting with Tim", artist "T. Turner">, <ResearchProject: id 3, topic "Swallow Aerodynamics", supervisor "Dr. Winter"> ]
使用vanilla django,我们得到基类对象,这很少是我们想要的:
>>> Project.objects.all() [ <Project: id 1, topic "Department Party">, <Project: id 2, topic "Painting with Tim">, <Project: id 3, topic "Swallow Aerodynamics"> ]
当多态模型通过 外键,多个场或单场。
功能
- 完全的管理集成。
- ORM集成:
- support for ForeignKey, ManyToManyField, OneToOneField descriptors.
- Filtering/ordering of inherited models (^{tt2}$).
- Filtering model types: ^{tt3}$ and ^{tt4}$
- Combining querysets of different models (^{tt5}$)
- Support for custom user-defined managers.
- 使用获取继承模型所需的最小查询量。
- 需要时禁用多态行为。
虽然django多态使子类模型易于在django中使用, 我们仍然鼓励谨慎使用它们。每个子类模型都需要 django执行INNER JOIN从数据库中获取模型字段。 考虑到这一点,使用子类模型是有正当理由的。 这就是这个图书馆的设计宗旨!
有关详细信息,请参见documentation at Read the Docs。
许可证
django多态使用与django相同的许可证(类似于bsd)。