django - update a queryset in a specific order -



django - update a queryset in a specific order -

i have "index" field stores consequent range of integers, safety set unique.

now want increment field one, maintain unique update value in descending order:

mymodel.objects.all().order_by('-index').update(index=f('index')+1)

what surprises me on machine integrityerror gets raised , complains duplicated index value.

is there missed? save records 1 one?

thanks in advance!

update:

i think root problem there no order in sql update command (see update order by, , sql server: update table using order by)

obviously django translates statement sql update order_by, leads undefined behavior , creates different result per machine.

you ordering queryset ascending. can create descending adding '-' field name in order by:

mymodel.objects.all().order_by('-index').update(index=f('index')+1)

django docs order_by

django order django-queryset

Comments

Popular posts from this blog

Using Android Volley to post an array to PHP -

python - How to add an model instance to a django queryset? -

angularjs - No 'Access-Control-Allow-Origin' error from local domain to public API -