views:

48

answers:

1

Hi Guys,

I have a Django view called change_priority. I'm posting a request to this view with a commas separated list of values which is basically the order of the items in my model. The data looks like this:

1,4,11,31,2,4,7

I have a model called Items which has two values - id and priority. Upon getting this post request, how can I set the priority of the Item depending upon the list order. So my data in the db would look like.

1,1
4,2
11,3
31,4
2,5
4,6
7,7

Thanks guys.

+2  A: 

You don't really give much detail about how exactly you're receiving the information, but here's a stab at it.

order = data.split(',') # convert data string to a list
objects = MyModel.objects.in_bulk(order)
for i, id in enumerate(order):
    obj = objects['id']
    obj.priority = i
    obj.save()
Daniel Roseman
Hi Daniel. This looks like pretty good. Thanks.
Mridang Agarwalla