我有以下模型结构:
class Container(models.Model):
pass
class Generic(models.Model):
name = models.CharacterField(unique=True)
cont = models.ManyToManyField(Container, null=True)
# It is possible to have a Generic object not associated with any container,
# thats why null=True
class Specific1(Generic):
...
class Specific2(Generic):
...
...
class SpecificN(Generic):
...
比如说,我需要检索Specific
与特定 Container 有关系的所有类型模型。
用于此的 SQL 或多或少是微不足道的,但这不是问题所在。不幸的是,我在使用 ORM(特别是 Django 的 ORM)方面不是很有经验,所以我可能在这里遗漏了一个模式。
当以蛮力方式完成时, -
c = Container.objects.get(name='somename') # this gets me the container
items = c.generic_set.all()
# this gets me all Generic objects, that are related to the container
# Now what? I need to get to the actual Specific objects, so I need to somehow
# get the type of the underlying Specific object and get it
for item in items:
spec = getattr(item, item.get_my_specific_type())
这会导致大量的数据库命中(每个通用记录一个,与容器相关),所以这显然不是这样做的方法。现在,也许可以通过直接获取 SpecificX 对象来完成:
s = Specific1.objects.filter(cont__name='somename')
# This gets me all Specific1 objects for the specified container
...
# do it for every Specific type
这样,对于每种特定类型,数据库都会被命中一次(我猜是可以接受的)。
我知道, .select_related() 不适用于 m2m 关系,因此在这里没有太大帮助。
重申一下,最终结果必须是一组 SpecificX 对象(而不是 Generic)。