本文介绍了切片后无法更新查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试这样做:
UserLog.objects.filter(user=user).filter(action='message').filter(timestamp__lt=now)[0:5].update(read=True)
但我遇到此错误:
Cannot update a query once a slice has been taken.
(使用Django 1.2.1)
(using django 1.2.1)
我在做什么错了?
推荐答案
由于错误状态,您无法调用 update()
在QuerySet上,如果您取出了一个切片。
As the error states, you cannot call update()
on a QuerySet if you took out a slice.
原因:
- 进行切片相当于SQL中的
LIMIT
语句。 - 发出更新会将查询转变为
UPDATE
语句。
- Taking a slice is equivalent to a
LIMIT
statement in SQL. - Issuing an update turns your query into an
UPDATE
statement.
您要执行的操作等同于
更新...位置...限制5
这是不可能的,至少在标准SQL中是不可能的。
which is not possible, at least not with standard SQL.
这篇关于切片后无法更新查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!