SQL 使用单个查询集选择和更新数据库记录
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2712682/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
select and update database record with a single queryset
提问by John
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
如何在同一条语句上运行update
andselect
语句,queryset
而不必执行两个查询: - 一个选择对象 - 另一个更新对象
The equivalent in SQL would be something like:
SQL 中的等效项类似于:
update my_table set field_1 = 'some value' where pk_field = some_value
回答by Daniel Roseman
Use the queryset object update
method:
使用查询集对象update
方法:
MyModel.objects.filter(pk=some_value).update(field1='some value')
回答by Slipstream
Django database objects use the same save() method for creating and changing objects.
Django 数据库对象使用相同的 save() 方法来创建和更改对象。
obj = Product.objects.get(pk=pk)
obj.name = "some_new_value"
obj.save()
How Django knows to UPDATE vs. INSERT
If the object's primary key attribute is set to a value that evaluates to True (i.e., a value other than None or the empty string), Django executes an UPDATE. If the object's primary key attribute is not set or if the UPDATE didn't update anything, Django executes an INSERT.
Django 如何知道 UPDATE 与 INSERT
如果对象的主键属性设置为一个评估为 True 的值(即,非 None 或空字符串的值),Django 将执行 UPDATE。如果对象的主键属性未设置或 UPDATE 未更新任何内容,Django 将执行 INSERT。
Ref.: https://docs.djangoproject.com/en/1.9/ref/models/instances/
参考:https: //docs.djangoproject.com/en/1.9/ref/models/instances/
回答by Pransh Tiwari
This answer compares the above two approaches. If you want to update many objects in a single line, go for:
这个答案比较了上述两种方法。如果要在一行中更新多个对象,请执行以下操作:
# Approach 1
MyModel.objects.filter(field1='Computer').update(field2='cool')
Otherwise you would have to iterate over the query set and update individual objects:
否则,您将不得不遍历查询集并更新单个对象:
#Approach 2
objects = MyModel.objects.filter(field1='Computer')
for obj in objects:
obj.field2 = 'cool'
obj.save()
Approach 1 is faster because, it makes only one database query, compared to approach 2 which makes 'n+1' database queries. (For n items in the query set)
Fist approach makes one db query ie UPDATE, the second one makes two: SELECT and then UPDATE.
The tradeoff is that, suppose you have any triggers, like updating
updated_on
or any such related fields, it will not be triggered on direct update ie approach 1.Approach 1 is used on a queryset, so it is possible to update multiple objects at once, not in the case of approach 2.
方法 1 更快,因为它只进行一次数据库查询,与进行“n+1”个数据库查询的方法 2 相比。(对于查询集中的 n 个项目)
第一种方法进行一个 db 查询,即 UPDATE,第二种方法进行两个:SELECT 和 UPDATE。
权衡是,假设您有任何触发器,例如更新
updated_on
或任何此类相关字段,它不会在直接更新时触发,即方法 1。方法 1 用于查询集,因此可以一次更新多个对象,而不是方法 2 的情况。
回答by Jamil Noyda
only in a case in serializer
things, you can update in very simple way!
只有在serializer
某些情况下,您才能以非常简单的方式更新!
my_model_serializer = MyModelSerializer(
instance=my_model, data=validated_data)
if my_model_serializer.is_valid():
my_model_serializer.save()
only in a case in form
things!
只有在form
事情的情况下!
instance = get_object_or_404(MyModel, id=id)
form = MyForm(request.POST or None, instance=instance)
if form.is_valid():
form.save()