在django中的父类中的对象中查找子类中的对象 [英] Find object in child class from object in parent class in django

查看:107
本文介绍了在django中的父类中的对象中查找子类中的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个父类(ThingsThatMigrate)和两个孩子(椰子和燕子)。现在我说我有一个ThingsThatMigrate对象。如何确定它是否实际上是椰子或燕子?一旦这样做,我该怎么去Coconut或Swallow对象?

解决方案

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆