在对基类进行查询后确定 Django 模型实例类型 [英] Determining Django Model Instance Types after a Query on a Base-class

查看:22
本文介绍了在对基类进行查询后确定 Django 模型实例类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在从对基类的查询返回之后,有没有办法确定 Django 数据库对象的真实"类是什么?

例如,如果我有这些模型......

class Animal(models.Model):名称=models.CharField(max_length=128)类人(动物):裤子尺寸 = models.IntegerField(null=True)类狗(动物):panting_rate = models.IntegerField(null=True)

并创建这些实例...

Person(name='Dave').save()狗(名字='拉夫斯沃思先生').保存()

如果我执行像 Animal.objects.all() 这样的查询,我最终会得到两个 Animal 实例,而不是 Person 的实例> 和 Dog 的一个实例.有没有办法确定哪个实例属于哪种类型?

<小时>

仅供参考:我已经尝试过这样做...

isinstance(Animal.objects.get(name='Dave'),Person) # <-- 返回假!

但这似乎不起作用.

解决方案

我以前也遇到过类似的问题,最终在 这个答案.

通过实现一个抽象类来存储real 类并让它由您的父类继承,一次可以将每个父类实例转换为实际类型.(该答案中使用的抽象类现在可以在 django-model-utils.)

例如,一旦你定义了抽象类(或者如果你有 django-model-utils),你可以简单地做:

class Animal(InheritanceCastModel):名称=models.CharField(max_length=128)类人(动物):裤子尺寸 = models.IntegerField(null=True)类狗(动物):panting_rate = models.IntegerField(null=True)

使用起来很简单:

<预><代码>>>>从 zoo.models 导入动物、人、狗>>>动物(名称='马尔科姆').保存()>>>人(姓名=戴夫").保存()>>>狗(名字='拉夫斯沃思先生').保存()>>>对于 Animal.objects.all() 中的 obj:... 打印 obj.name, type(obj.cast())...马尔科姆 戴夫 拉夫斯沃思先生<class 'zoo.models.Dog'>

Is there a way to determine what the 'real' class of a Django database object is, after it has been returned from a query for on a base class?

For instance, if I have these models...

class Animal(models.Model):
    name= models.CharField(max_length=128)

class Person(Animal):
    pants_size = models.IntegerField(null=True)

class Dog(Animal):
    panting_rate = models.IntegerField(null=True)

And create these instances...

Person(name='Dave').save()
Dog(name='Mr. Rufflesworth').save()

If I do a query like Animal.objects.all(), I end up with two Animal instances, not an instance of Person and an instance of Dog. Is there any way to determine which instance is of which type?


FYI: I already tried doing this...

isinstance(Animal.objects.get(name='Dave'),Person) # <-- Returns false!

But that doesn't seem to work.

解决方案

I had a similar problem in the past and eventually found a satisfactory solution thanks to this answer.

By implementing an abstract class that stores the real class and have it inherited by your parent class, once can cast each parent class instance to the actual type. (The abstract class used in that answer is now available in django-model-utils.)

For example, once you have the abstract class defined (or if you have django-model-utils), you can simply do:

class Animal(InheritanceCastModel):
    name= models.CharField(max_length=128)

class Person(Animal):
    pants_size = models.IntegerField(null=True)

class Dog(Animal):
    panting_rate = models.IntegerField(null=True)

Using it is trivial:

>>> from zoo.models import Animal, Person, Dog
>>> Animal(name='Malcolm').save()
>>> Person(name='Dave').save()
>>> Dog(name='Mr. Rufflesworth').save()
>>> for obj in Animal.objects.all():
...     print obj.name, type(obj.cast())
...
Malcolm <class 'zoo.models.Animal'>
Dave <class 'zoo.models.Person'>
Mr. Rufflesworth <class 'zoo.models.Dog'>

这篇关于在对基类进行查询后确定 Django 模型实例类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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