在django中的父类中的对象中查找子类中的对象 [英] Find object in child class from object in parent class in django
问题描述
Django不提供这样的模型多态性的最简单的方法来做你想要实现的是将新对象的内容类型存储在其中。有一个简单的通用应用程序,称为 django-polymorphics-models ,为您提供此功能,另外还有一个 downcast
- 将返回子对象的方法!
Let's say I have a parent class (ThingsThatMigrate) and two children (Coconut and Swallow). Now let's say I have a ThingsThatMigrate object. How can I determine if it is in fact a Coconut or a Swallow? Once having done so, how can I get to the Coconut or Swallow object?
Django doesn't offer such model polymorphism out of the box.The easiest way to do what you are trying to achieve is to store the content type of a new object in it. There's a simple generic app called django-polymorphic-models which offers you this functionality - and - additionally a downcast
-method that will return the child object!
这篇关于在django中的父类中的对象中查找子类中的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!