1
我有以下ModelResource:Tastypie優化與prefetch_related和ModelResources的
class DivisionsResource(ModelResource):
plants = fields.ToManyField('plants.api.resources.PlantsResource', 'plant_set', full = True)
class Meta:
queryset = Division.objects.all().prefetch_related('plant_set')
allowed_method = ['get']
filtering = {
"name": ('istartswith')
}
class PlantsResource(ModelResource):
picture = fields.ToOneField('files.api.resources.PlantPictureResource', 'picture', full=True)
production_lines = fields.ToManyField('production_lines.api.resources.ProductionLinesResource','productionline_set', full=True, null=True)
class Meta:
queryset = Plant.objects.select_related('picture').all().prefetch_related('productionline_set')
allowed_methods = ['get']
然而,如果我所說的「分裂」的資源,我看看SQL查詢,它不會做的prefetch_related和內select_related 植物資源。它會爲每個工廠選擇productionline_set和picture,而不是在 SQL查詢中執行。這是爲什麼?然而
我發現我可以做這樣的事情:
queryset = Division.objects.all().prefetch_related('plant_set', 'plant_set__picture','plant_set__productionline_set')
我必須肯定在司型號做到這一點?如果在父母打電話時不會調用「prefetch」和「select_related」,將會超長。