Answer the question
In order to leave comments, you need to log in
How to update the number of products after saving the form?
Good day, dear connoisseurs)
There is a product. It is sold in two stores with one warehouse. The model has a "quantity in stock" field, but it needs to be the same for two stores. In the admin panel (Django-Oscar), I removed the ability to directly edit the quantity of goods in stock for an individual store. Instead, I added a new field in which you need to enter how many goods to add.
I thought to solve it like this:
Answer the question
In order to leave comments, you need to log in
Solution:
As advised, the quantity of goods in the warehouse was placed in a separate field for the goods (and prices are stored in stockrecord). In stockrecord, the quantity is made a reference to the quantity of the product.
class Product(AbstractProduct):
...
num_in_stock_product = models.PositiveIntegerField(
_("Number in stock(product)"), blank=True, null=True)
stock_update = models.IntegerField(
_("Stock update"), blank=True, null=True)
def save(self, *args, **kwargs):
if self.stock_update:
if not self.num_in_stock_product:
self.num_in_stock_product = 0
self.num_in_stock_product += self.stock_update
self.stock_update = None
super(AbstractProduct, self).save(*args, **kwargs)
self.attr.save()
class StockRecord(AbstractStockRecord):
...
@property
def num_in_stock_stockrecord(self):
return self.product.num_in_stock_product
@property
def net_stock_level(self):
"""
The effective number in stock (eg available to buy).
This is correct property to show the customer, not the num_in_stock
field as that doesn't account for allocations. This can be negative in
some unusual circumstances
"""
all_allocations = 0
for stockrecord in self.product.stockrecords.all():
if stockrecord.num_allocated is not None:
all_allocations += stockrecord.num_allocated
if self.num_in_stock_stockrecord is None:
return 0
return self.num_in_stock_stockrecord - all_allocations
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question