select and update database record with a single queryset

John picture John · Apr 26, 2010 · Viewed 184.3k times · Source

How do I run an update and select statements on the same queryset rather than having to do two queries: - one to select the object - and one to update the object

The equivalent in SQL would be something like:

update my_table set field_1 = 'some value' where pk_field = some_value

Answer

Daniel Roseman picture Daniel Roseman · Apr 26, 2010

Use the queryset object update method:

MyModel.objects.filter(pk=some_value).update(field1='some value')