Answers for "django model query add annotation field to show duplicate count"

1

django model query add annotation field to show duplicate count

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')))
Posted by: Guest on March-02-2021

Python Answers by Framework

Browse Popular Code Answers by Language