我有这个查询集:
brand = get_object_or_404(Brand, shortcut=shortcut)
ads = Ad.objects.filter(mod__submodel__model__brand = brand).values('id')
但由于某种原因,django 执行相同的查询 5 次(查询数不取决于行数)。就这个:
SELECT ••• FROM "catalog_ad" INNER JOIN "catalog_mod" ON ("catalog_ad"."mod_id" = "catalog_mod"."id") INNER JOIN "catalog_submodel" ON ("catalog_mod"."submodel_id" = "catalog_submodel"."id") INNER JOIN "catalog_model" ON ("catalog_submodel"."model_id" = "catalog_model"."id") WHERE "catalog_model"."brand_id" = 1 LIMIT 21
那么为什么 django 会执行 5 次相同的查询呢?
更新:这只发生在我ads
像这样传递给上下文处理器时:
return render(request, 'cat.html', {ads:ads})