python - Django: Update order attribute for objects in a queryset -


I have a feature to order objects on my model. I have to update the order of the element based on a list , In which the object ID is included in the new order; Right now I am running on top of the whole search and after the second I am setting up an object. What would be the easiest / fastest way to do this with the full query?

  def update_ordering (model, order): "order" [id, id, id, id] For example: [8,4,5,1,3] "" "id_to_order = Dict ((order [i], i) for class (lane (sequence)) for model.objects.all in x (): x.order = id_to_order [x.id] x.save ()  

This can not be done in a single quarantined operation. As far as I know That this can not be done in a query with the raw SQL, so you always need to update each The update will require an update call, so both of you and Colin Anderson's solutions are very well suited for your description.

However, what are the issues with your use? Do you really change the entire list every time? In most cases this is not very likely. I can see some different perspectives.

You say that like saving the order field, but you become a difference for the order list They are: def

  update_ordering (model, order): "" "order is in the example [id, id, id, id]: [8,4,5,1,3] "" Original_order = model.object.live_list ('id', flat = true)). Order_by ('order') Order = Filter (Lambda x: x [1]! = X [2], ZIP (xchange (lane) (order)), order, original_order: I order: model.objects = I [1]). Update (order = i [0])  

There is a partial update based on another approach you are doing (such as using AJAX) if possible then complete : Instead of updating the ordered set, just update each update separately, it often increases the total load, but more expands over time. For example, running 5th element step-by-step to place 2 places, it will present 3 swaps: (5,4); (4,3); (3,2). As a result of 6 updates, only 4 will be required with all-in-one-time approach, but small operations will be spread over time.


Comments

Popular posts from this blog

windows - Heroku throws SQLITE3 Read only exception -

lex - Building a lexical Analyzer in Java -

python - rename keys in a dictionary -