I used prefetch_related
with Prefetch
:
prefetch_qs = Offer.objects.filter(price__gt=1000)
prefetch = Prefetch('offers', queryset=prefetch_qs)
How can I exclude rows with empty offers? It doesn't work, because annotate counted all offers (not filtered in prefetch
):
filtered_qs = Product.objects.annotate(
offers_count=Count('offers')
).filter(
offers_count__gt=0
).prefetch_related(
prefetch
)
补充@Todor答案:您可以创建自定义子查询类型以简化第二种方法并允许重用。
The
Prefetch
is being executed as a 2nd query after the products query, thus its not possible to filter out products based on the prefetch. You need to repeat the prefetch filtration as either Subquery or inside the Count which you are trying to make.为了使计数工作,请尝试以下操作:
为了对子查询执行此操作,您需要这样的操作:
The Subquery approach may look a little bit hard to understand why its done this way, I've tried to explain it in some old question here