如何使用 Django 中的单个查询集选择记录并更新它? [英] How to select a record and update it, with a single queryset in Django?
本文介绍了如何使用 Django 中的单个查询集选择记录并更新它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在同一个 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')
这篇关于如何使用 Django 中的单个查询集选择记录并更新它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文