save() on a model instance not update the changed field on related model side

Desmond Chen

My models:

class InventoryItem(models.Model):
    quantity = models.IntegerField()
    ...

class Requisition(models.Model):
    from_inventoryitem = models.ForeignKey(InventoryItem)
    quantity = models.IntegerField()
    ...

Assuming that requisition has an OneToMany relation with inventory_item below, and the initial value of inventory_item.quantity is 0, When I excute:

>>> requisition = Requisition.objects.get(id=1)
>>> requisition.from_inventoryitem.quantity = 500
>>> requisition.save()

>>> requisition.from_inventoryitem.quantity
500
>>> inventory_item.quantity
0

>>> requisition.from_inventoryitem == inventory_item
True

The inventory_item.quantity in database and InventoryItem side is still 0. How can I update that change to the database?

Thomas

This is by design. You have to save each instance seperately. requisition.from_inventoryitem actually queries the InventoryItem instance from the database, just to set the quantity.

requisition = Requisition.objects.get(id=1)
requisition.from_inventoryitem.quantity = 500 # this generates another query here!
requisition.from_inventoryitem.save()

or even better, with a single query, single save

inv_item = InventoryItem.objects.get(requisition_set__id=1)
inv_item.quantity = 500
inv_item.save()

best way. single database call:

InventoryItem.objects.filter(requisition_set__id=1).update(quantity=500)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Related models update on model field change

From Dev

Run before_save after changed in attribute in a related model

From Dev

Run before_save after changed in attribute in a related model

From Dev

Create a field in form for each model instance related to another instance in django

From Dev

Django model save related model

From Dev

Django Rest Framework how to save a model with Related Field based on ID

From Dev

Save method of Django model's instance does not update fields

From Dev

Better way to duplicate model instance and related model

From Dev

Update Model only changed properties

From Dev

Update automatically a specific field in Django model when saving other many-to-many related model

From Dev

Laravel Update Related Model with push?

From Dev

Creating a model instance on post save

From Dev

Create instance of model with field with choices

From Dev

Save a list of objects in model as a field

From Dev

Save calculated value as a model field

From Dev

Django update model field based on other model

From Dev

Django update model field based on other model

From Dev

Django update model instance asynchronously

From Dev

Rails update related model data in update method

From Dev

MVVM - Validating the model when a field in the viewmodel is changed

From Dev

ng-model not updating on datetime field changed

From Dev

Update Django Model With Form Field

From Dev

Laravel Required Field on Model Update

From Dev

Laravel Required Field on Model Update

From Dev

How to update the view after the model has changed?

From Dev

How to update the view after the model has changed?

From Dev

Update template with model changed from input vueJS

From Dev

Foreign Keys clash with related field in Django Model

From Dev

Get specific related model field in laravel

Related Related

  1. 1

    Related models update on model field change

  2. 2

    Run before_save after changed in attribute in a related model

  3. 3

    Run before_save after changed in attribute in a related model

  4. 4

    Create a field in form for each model instance related to another instance in django

  5. 5

    Django model save related model

  6. 6

    Django Rest Framework how to save a model with Related Field based on ID

  7. 7

    Save method of Django model's instance does not update fields

  8. 8

    Better way to duplicate model instance and related model

  9. 9

    Update Model only changed properties

  10. 10

    Update automatically a specific field in Django model when saving other many-to-many related model

  11. 11

    Laravel Update Related Model with push?

  12. 12

    Creating a model instance on post save

  13. 13

    Create instance of model with field with choices

  14. 14

    Save a list of objects in model as a field

  15. 15

    Save calculated value as a model field

  16. 16

    Django update model field based on other model

  17. 17

    Django update model field based on other model

  18. 18

    Django update model instance asynchronously

  19. 19

    Rails update related model data in update method

  20. 20

    MVVM - Validating the model when a field in the viewmodel is changed

  21. 21

    ng-model not updating on datetime field changed

  22. 22

    Update Django Model With Form Field

  23. 23

    Laravel Required Field on Model Update

  24. 24

    Laravel Required Field on Model Update

  25. 25

    How to update the view after the model has changed?

  26. 26

    How to update the view after the model has changed?

  27. 27

    Update template with model changed from input vueJS

  28. 28

    Foreign Keys clash with related field in Django Model

  29. 29

    Get specific related model field in laravel

HotTag

Archive