获取抽象基础模型类的子模型的实例

时间:2019-07-15 12:19:29

标签: python django django-queryset

我有一个BaseModel类和它的三个孩子。我想要一个查询集,该查询集将所有子类的所有实例组合到我的BaseNotification类中。有没有办法在一个查询中做到这一点?

class BaseNotification(BaseModel):
    seen = models.BooleanField(default=False)
    text = models.TextField()

    class Meta:
        abstract = True


class ApprovalNotification(BaseNotification):
    object = models.ForeignKey("Object", on_delete=models.CASCADE, null=True, blank=True)
    user = models.ForeignKey("User", on_delete=models.CASCADE)


class ComplaintNotification(BaseNotification):
    complaint = models.ForeignKey("Complaint", on_delete=models.CASCADE)


class ProposalNotification(BaseNotification):
    proposal = models.ForeignKey("Proposal", on_delete=models.CASCADE)

当我尝试python BaseNotification.objects.all()时,显然会出现一个错误,即BaseNotification没有属性'objects'。

0 个答案:

没有答案