我的Django 2.2项目中具有以下模型:
class ModelA(models.Model):
ModelA_id = models.UUIDField('ModelA ID', default = uuid.uuid4)
class ModelB(models.Model):
ModelB_id = models.UUIDField('ModelB ID', default = uuid.uuid4)
ModelA = models.ForeignKey(ModelA, on_delete=models.CASCADE)
class ModelC(models.Model):
ModelC_id = models.UUIDField('ModelC ID', default = uuid.uuid4)
ModelB = models.ForeignKey(ModelB, on_delete=models.CASCADE,)
class ModelD(models.Model):
ModelD_id = models.UUIDField('ID', default = uuid.uuid4)
ModelC = models.ForeignKey(ModelC, on_delete=models.CASCADE)
user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
如您所见,模型的嵌套方式如下:
ModelD has FK to ModelC
ModelC has FK to ModelB
ModelB has FK to ModelA
如果我这样做:
ModelD.objects.filter(pk=0).select_related('ModelC__ModelB__ModelA')
这是否意味着在单个(或有限的)数据库请求中一次查询所有数据?我认为这也意味着它将所有从ModelD开始的数据引用都放入内存中,对吗?
我的最终目标是总结有多少ModelD引用ModelC,有(没有多少)ModelC引用ModelB,最后是有哪些ModelB引用ModelA。