Cannot update a query once a slice has been taken

后端 未结 6 727
南笙
南笙 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:25

    Your code is incorrect because of where the slicing happens. It should happen after the call to update(), not before.

    Wrong:

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

    Right:

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

提交回复
热议问题