使用单个查询集选择和更新数据库记录
问题描述:
如何在同一queryset
上运行update
和select
语句,而不必执行两个查询:
-一个选择对象
-和一个更新对象
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
SQL中的等效内容如下:
The equivalent in SQL would be something like:
update my_table set field_1 = 'some value' where pk_field = some_value
答
使用查询集对象update
方法:
Use the queryset object update
method:
MyModel.objects.filter(pk=some_value).update(field1='some value')