Features: 1) Add has_stock and has_price annotations for stock and price availability checks.

Fixes: 1) Correct conditional logic in `personal_order_only` annotation to use integers for comparison.

Extra: 1) Minor readability improvements in annotation definitions.
This commit is contained in:
Egor Pavlovich Gorbunov 2025-09-16 15:46:25 +03:00
parent 91b043ac4f
commit fe6316dae1

View file

@ -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(),
)