如何 prefetch_related fields for a related field in Django

How to prefetch_related fields for a related field in Django

我似乎有一些例子说明如何在 Django 的前向和后向关系中 prefetch_related 字段,但我怀疑如果我们想要预取相关模型的所有字段。

例如,如果我想从以下模型中获取所有内容,使用 HealthCheck 作为起点。哪个查询是实现该目标的最优化查询?

class HealthCheck(models.Model):
    id = models.Integer()
    person = models.ForeignKey('Person')


class Person(models.Model):
    profile = models.ForeignKey('Profile')
    vaccines = models.ManyToManyField('vaccines', through='PersonVaccines')


class Profile(models.Model):
    name = models.CharField(max_length=16)


class PersonVaccines(models.Model):
    person = models.ForeignKey(Person)
    vaccine = models.ForeignKey('Vaccine')


class Vaccine(models.Model):
    name = models.CharField(max_length=16)

我试过类似的方法,但似乎不起作用:

from django.db.models import Prefetch

HealthCheck.objects.filter(id=1).prefetch_related(
    Prefetch(
        'person__vaccines',
        queryset=PersonVaccines.objects.select_related('person', 'person__profile', 'vaccine')
    )
)

如何预取所有相关内容?

应该可以:

HealthCheck.objects.filter(id=1).select_related('person__profile').prefetch_related('person__vaccines')

select_related 将获取所有相关字段,直到 profile

prefetch_related 然后应该预取所有相关对象,直到 vaccines

编辑:测试并确认当我为一个健康检查人员创建 3 个疫苗对象时只生成 2 个查询(n 是我计算执行的查询的函数):

In [2]: health_check = HealthCheck.objects.filter(id=3).select_related('person__profile').prefetch_related('person__vaccines').get()
     ...:

In [3]: n()
2

In [4]: for v in health_check.person.vaccines.all():
     ...:     print(v.name)
     ...:
v1
v2
v3

In [5]: n()
0