Manish Gupta Manish Gupta - 1 year ago 78
Ajax Question

multiple object querydict to lists

I am sending some javascript objects as array of objects to django view via ajax. The object is like:


My ajax:

type : "POST",
data : {action:'pack_orders',

I can see the request in django view as QueryDict like:

<QueryDict : {'order_dict[0][oid]':'id1','order_dict[0][oiid]':'iid1',

How can i get lists from querydict in django view like:

oid_list = ['id1','id2']
oiid_list = ['iid1','iid2']

Output in



1) Added output from statement
print dict(request.POST)

Answer Source

I am not sure if you are sending order_dict after JSON.stringifying it. If not, I suggest do it. Accept it as json string in your view, do a json.loads() on it. Post this you have a list of dictionary.

Lets say you store it in variable called data, then you can do as following

oid_list = [x.get('oid') for x in data]
oiid_list = [x.get('oiid') for x in data]


oid_list - ['id1', 'id2']
oiid_list - ['iid1', 'iid2']

Should work for you. I dont know if its the best solution, but a simple one.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download