Advertisement
object_254

history product filter

Aug 27th, 2022
780
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.71 KB | None | 0 0
  1. import operator
  2. from functools import reduce
  3. from django.db.models import Q, F, Sum, Count, Max, Min, IntegerField
  4. from django.db.models.functions import Coalesce
  5. from product_storage.models.product_history import ProductPriceHistory
  6. from product_storage.models.products import Product
  7.  
  8. product_with_max_increase = (
  9.     ProductPriceHistory.objects
  10.     .values('product')
  11.     .annotate(max_increase=Max('percent'))
  12.     .order_by('-percent', '-created_at')
  13.     )
  14.  
  15. filter_object = reduce(operator.or_, (Q(**x) for x in product_with_max_increase))
  16.  
  17. queryset = (
  18.     ProductPriceHistory.objects
  19.     .annotate(max_increase=Max('percent'))
  20.     .filter(filter_object)
  21.     .order_by('-max_increase')
  22.     )[:20]
  23.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement