Succinct way of updating a single field of a django model object

django, django-models

Solution

Yup.

Product.objects.filter(name='Venezuelan Beaver Cheese').update(number_sold=4)

If you have a model instance you changed and want to save only specific fields to the database, do that:

product.name = "New name of the product"
product.save(update_fields=['name'])

Problem

To update (and save) the field on an object you do: ``` >>> product = Product.objects.get(name='Venezuelan Beaver Cheese') >>> product.number_sold = 4 >>> product.save() ``` Is there a way to compress the last two lines into a single line, like: ``` product.update(number_sold=4) ```

Original source