且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Django模型子类化:通过查询超类获取子类

更新时间:2023-12-02 21:34:58

您应该检查解决方案由Ca发布rl Meyer 前一段时间。它内部使用ContentType方法,但它非常优雅地封装了它。

You should check the solution posted by Carl Meyer some time ago. It internally uses the ContentType approach, but it encapsulates it very elegantly .

他还指出了一种替代的,更有效的解决方案,不需要存储ad字段,但它只适用于直接的子类。如果您有几个继承级别,则第一个解决方案会更好。

He also points to an alternative, and more efficient solution, that doesn't need to store an aditional field on the database, but it will only work for direct child classes. If you have several inheritance levels, the first solution is better.