feat(models): refine price aggregation to include active stocks

update `min_price` and `max_price` methods to consider only active stocks in price aggregation. This ensures more accurate price calculations by filtering out inactive stock entries.
This commit is contained in:
Egor Pavlovich Gorbunov 2026-02-27 21:58:41 +03:00
parent b1382b09c2
commit 7bb05d4987

View file

@ -464,14 +464,18 @@ class Category(NiceModel, MPTTModel):
@cached_property @cached_property
def min_price(self) -> float: def min_price(self) -> float:
return ( return (
self.products.filter(is_active=True).aggregate(Min("price"))["price__min"] self.products.filter(
is_active=True, stocks__is_active=True
).aggregate(Min("stocks__price"))["stocks__price__min"]
or 0.0 or 0.0
) )
@cached_property @cached_property
def max_price(self) -> float: def max_price(self) -> float:
return ( return (
self.products.filter(is_active=True).aggregate(Max("price"))["price__max"] self.products.filter(
is_active=True, stocks__is_active=True
).aggregate(Max("stocks__price"))["stocks__price__max"]
or 0.0 or 0.0
) )