0

I would like to get the specific information from different model therefore I have use the method of select_related to fetch the data but I unable to save the record into db. How to use select_related in save method?

Let say I have following models models.py

Now I would like to get the product_quantity from stocklist and do a subtraction between product_quantity and shipping_qty(from stockOut models) to get the final quantity that remain.

admin.py

But I unable to save/update the value into db when I use the method of select_related to get the specific information. How would I go about doing this? Thanks.

joyce
  • 21
  • 1

1 Answers1

0

first of all you don't get only shipping_qty if you do stockOut.objects.all().select_related("shipping_qty") but you get all stockOut instances with their shipping_qty and stocks

what you want is, to get these values for only the current instance

def save_model(self, request, obj, form, change):
    stock = obj.stock
    shipping_qty = obj.shipping_qty
    if shipping_qty and stock.quantity_in_store:
        obj.shipping_qty = stokc.quantity_in_store - shipping_qty
    super().save_model(request, obj, form, change)
FabianClemenz
  • 303
  • 3
  • 8