gpt4 book ai didi

python - 如何找到 Django 模型基类的 "concrete class"

转载 作者:太空狗 更新时间:2023-10-29 18:01:12 29 4
gpt4 key购买 nike

在使用模型继承时,我试图找到 django 模型对象的实际类。

一些描述问题的代码:

class Base(models.model):
def basemethod(self):
...

class Child_1(Base):
pass

class Child_2(Base):
pass

如果我创建两个子类的各种对象并创建一个包含它们的查询集:

Child_1().save()
Child_2().save()
(o1, o2) = Base.objects.all()

我想确定对象在基方法中的类型是 Child_1 还是 Child_2,我可以通过 o1.child_1 和 o2.child_2 获取子对象,但这会重新获得有关基类中子类的知识。

我想出了以下代码:

def concrete_instance(self):
instance = None
for subclass in self._meta.get_all_related_objects():
acc_name = subclass.get_accessor_name()
try:
instance = self.__getattribute__(acc_name)
return instance
except Exception, e:
pass

但它感觉很脆弱,我不确定如果我在更多层次上继承会发生什么。

最佳答案

Django 在父模型表和子模型表之间使用 OneToOneField 实现模型继承。当您执行 Base.object.all() 时,Django 仅查询 Base 表,因此无法知道子表是什么。因此,不幸的是,如果不进行额外的查询,就不可能直接转到子模型实例。

snippet显示了向基本模型添加 ContentType 字段的常用方法:

from django.contrib.contenttypes.models import ContentType

class Base(models.Model):
content_type = models.ForeignKey(ContentType,editable=False,null=True)

def save(self):
if(not self.content_type):
self.content_type = ContentType.objects.get_for_model(self.__class__)
self.save_base()

def as_leaf_class(self):
content_type = self.content_type
model = content_type.model_class()
if(model == Base):
return self
return model.objects.get(id=self.id)

然后您可以说 if Base.content_type.model_class() 来确定类型。

Here是另一个将自定义管理器添加到组合中的片段。

如您所见,这两种解决方案都可能非常昂贵。如果您有大量实例,使用 as_leaf_class() 方法将需要对每个项目进行一次查询。

相反,如果您有一组已知的子模型,只需分别查询每个模型并将实例聚合到一个列表中。

关于python - 如何找到 Django 模型基类的 "concrete class",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/349206/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com