使用Django 1.7:
我的Queryset看起来像这样:
return super(EmployeeViewSet, self).get_queryset()\
.filter(status__deleted_flag=False, **filter_kwargs)\
.prefetch_related('phone_number_set', 'email_address_set', 'street_address_set',
Prefetch('file_set', EmployeeFile.objects.active().select_related('content_type')))\
.order_by('last_name', 'first_name')
DjDT显示执行的预取查询
SELECT ••• FROM "employees_employeefile" INNER JOIN "employees_employeefiletype"
ON ("employees_employeefile"."content_type_id" = "employees_employeefiletype"."id" )
WHERE ("employees_employeefile"."deleted_at" IS NULL
AND "employees_employeefile"."owner_id" IN (53, 81, ...))
ORDER BY "employees_employeefile"."created_at" DESC
然后,DjDT显示每个员工被重新获得相同的文件列表
SELECT ••• FROM "employees_employeefile"
WHERE ("employees_employeefile"."owner_id" = 53
AND "employees_employeefile"."deleted_at" IS NULL)
ORDER BY "employees_employeefile"."created_at" DESC
他们的查询看起来和我一样,用于执行选择的ModelManager方法与(EmployeeFile.objects.active())
vs employee.file_set.active()
另外,我尝试删除select_related('content_type')
以防万一是问题