是否可以对给定的模型继承进行查询查找?

2024-09-22 14:23:07 发布

您现在位置:Python中文网/ 问答频道 /正文

下面是我的模型,我使用的是InheritanceManagerdjango-model-utils。我决定字段user在子类中,因为我需要通过user.child1_set.all()这样的关系访问Child子类。你知道吗

class Mother(models.Model):
    objects = InheritanceManager()

class Child1(Mother):
    user = models.ForiegnKey(User)

class Child2(Mother):
    user = models.ForiegnKey(User)

问题是,当我想从Mother类开始进行查询时,似乎没有任何查询使它成为可能。你知道吗

我试过这些密码。你知道吗

Mother.objects.filter(user=SOMEUSER)
Mother.objects.filter(child1__user=SOMEUSER)
Mother.objects.select_subclasses().filder(user=SOMEUSER)
Mother.objects.select_subclasses().filder(child1__user=SOMEUSER)

如有任何建议,将不胜感激。你知道吗


Tags: objectsmodelsfilter子类selectclassusermother