django model query add annotation field to show duplicate count

Solutions on MaxInterview for django model query add annotation field to show duplicate count by the best coders in the world

showing results for - "django model query add annotation field to show duplicate count"
Stacey
02 Oct 2018
1from django.db.models import OuterRef, Subquery, Count, Min
2
3subquery = Product.objects.filter(bc_sku=OuterRef('bc_sku')).values('bc_sku')
4                          .annotate(dup_count=Count('*'), min_price=Min('product_price'))
5Product.objects.filter(product_type='good')
6               .annotate(dup_count=Subquery(subquery.values('dup_count')), 
7                         min_price=Subquery(subquery.values('min_price')))