如何在Django中使用单个查询集选择记录并对其进行更新? [英] How to select a record and update it, with a single queryset in Django?

查看:47
本文介绍了如何在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

推荐答案

使用查询集

Use the queryset object update method:

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

这篇关于如何在Django中使用单个查询集选择记录并对其进行更新?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆