-1

I am building an inventory system using Django, right now I am adding the functionality to place an order. What I want to do is to subtract the quantity ordered from the quantity in the database. For example if someone ordered 10boxes of chips and I have 11 in my stock once I place the order on web I want the web to subtract 10 boxes from the stock. I am not really sure how to do that. Any help is appreciated!

AliReda-M
  • 329
  • 1
  • 3
  • 12
  • You do: `11 - 10 = 1`. Is that what you're asking? Else you need to be more elaborate. Where exactly you are facing the problem. – Rohit Jain Jul 22 '20 at 07:34
  • No of course am not asking for that, what I mean is that i might enter any value other than 10 and whatever value I enter should be subtracted from the value in the database. – AliReda-M Jul 22 '20 at 07:39
  • Try searching for `F` function in Django ORM. That should solve your issue. Well, had you gone through the document, you would have found it. – Rohit Jain Jul 22 '20 at 07:41

1 Answers1

0

Assuming that the model name is Inventory, item_code is the identification code of the ordered item, and ordered_amount is the amount ordered from your web interface. I would do the following:

(1) retrieve the item from inventory

item = Inventary.objects.get(id=item_code)

(2) update amount

item.amount = item.amount - ordered_amount

(3) save new value for item

item.save()

You can find a similar question here.

  • It depends how you access the inventory, and you implement the save, as you can see here https://stackoverflow.com/questions/1645269/concurrency-control-in-django-model – Williams Rizzi Jul 22 '20 at 07:54