Skip to content
Advertisement

Auto update model value based on another value in this model django

I have a model called Products. This model has values like this

JavaScript

How can I change display to be False if stock is equal to 0 automatically, so when client buys last product in store display value will change from True to False and

JavaScript

will result in products that I want to show or are in stock.

I know that I can simply chain .filter() but I wonder if I can do in an elegant way

Advertisement

Answer

For what you said I think you have a view to decrease stock value

Add this logic to your view, after update the stock value:

JavaScript

Considering that your view (used to decrease stock value) already instantiated the target “product”. If that don’t help you, please, post your view.py, to give us more information.

Advertisement