diff --git a/core/filters.py b/core/filters.py index 6c57a58f..d9706a30 100644 --- a/core/filters.py +++ b/core/filters.py @@ -306,8 +306,23 @@ class ProductFilter(FilterSet): ) qs = qs.annotate( + has_stock=Max( + Case( + When(stocks__quantity__gt=0, then=Value(1)), + default=Value(0), + output_field=IntegerField(), + ) + ), + has_price=Max( + Case( + When(stocks__price__gt=0, then=Value(1)), + default=Value(0), + output_field=IntegerField(), + ) + ), + ).annotate( personal_order_only=Case( - When(has_stock=Value(0), has_price=Value(1), then=Value(True)), + When(has_stock=0, has_price=1, then=Value(True)), default=Value(False), output_field=BooleanField(), )