I have a simple django’s query set like:
qs = AModel.objects.exclude(state="F").order_by("order")
I’d like to use it as follows:
qs[0:3].update(state='F')
expected = qs[3] # throws error here
But last statement throws:
“Cannot update a query once a slice has been taken.”
How can I duplicate the query set?
It’s the first line throwing the error: you can’t do qs[0:3].update(). qs[0:3] is taking a slice; update() is updating the query.
update() is meant for bulk updates, resulting in SQL queries like
You’re trying to update the first three items according to “order”, but that can’t be done with this type of UPDATE–you can’t order or limit an SQL UPDATE. It needs to be written differently, eg.
but Django can’t do that for you.