3👍
✅
The first wish was to use window function, but unfortunately it is not allowed to combine annotate(Window(...))
and filter(...)
The answer is:
from django.db.models import OuterRef, Subquery, Count, Min
subquery = Product.objects.filter(bc_sku=OuterRef('bc_sku')).values('bc_sku')
.annotate(dup_count=Count('*'), min_price=Min('product_price'))
Product.objects.filter(product_type='good')
.annotate(dup_count=Subquery(subquery.values('dup_count')),
min_price=Subquery(subquery.values('min_price')))
You can find details on how this query works in docs
Source:stackexchange.com