'm trying to make a small modification to django lfs project, that will allow me to deactivate products with no stocks. Unfortunatelly I'm just beginning to learn python, so I have big trouble with its syntax. That's what I'm trying to do. I'm using method 'is_variant' returning tru if my product is a sub type. If it is a variant I'm turning to parent product, get it's active variants and check their stocks. If stock is more than 0 variable active is 0, else it is 1. If after looping through variants 'active' is still 1 I set parent product's active to false.
I somehow cannot make it work the proper way. When using :
def deactivate(self):
if self.is_variant():
prod = self.parent
prod.active = all(var.get_stock_amount() != 0 for var in prod.variants.filter(active=True))
else:
prod.active = self.get_stock_amount() != 0
self.parent.save()
It deactivates my product no matter if it's variants have stocks or not. And when using :
inactive = 0
if self.is_variant():
prod = self.parent
for s in prod.variants.filter(active=True):
if s.get_stock_amount() == 0:
inactive = 1
else:
inactive = 0
if inactive == 1:
prod.active = 0
prod.save()
else:
if self.get_stock_amount() == 0:
self.active = 0
self.save()
The same happens, so my product is deactivated each time.
I've checked return types in shell and self is a variant and it is active.