Cannot update a query once a slice has been taken

后端 未结 6 706
南笙
南笙 2020-12-09 02:51

I am trying to do this:

UserLog.objects.filter(user=user).filter(action=\'message\').filter(timestamp__lt=now)[0:5].update(read=True)

but I

6条回答
  •  不思量自难忘°
    2020-12-09 03:19

    As the error states, you cannot call update() on a QuerySet if you took out a slice.

    The reason:

    1. Taking a slice is equivalent to a LIMIT statement in SQL.
    2. Issuing an update turns your query into an UPDATE statement.

    What you are trying to do would be equivalent to

    UPDATE ... WHERE ... LIMIT 5

    which is not possible, at least not with standard SQL.

提交回复
热议问题