Avoiding Django QueryDict List Constraints

I am trying to send data from a webpage to a django view that will be saved as serialized json for the database. If possible, I would like to avoid the django QueryDict object and just read the query using simplejson, smooth and save to the database. What is the best way to send data, so simplejson can smooth it out?

var languages = {}; languages['english'] = ['mark', 'james']; languages['spanish'] = ['amy', 'john']; $.ajax({ type: 'POST', url: '/save/', data: languages, dataType: 'json' }); 

.

 if request.is_ajax() and request.method == 'POST': for key in request.POST: print key valuelist = request.POST.getlist(key) print valuelist 
+4
source share
3 answers

I doubt that you can make django to avoid creating a QueryDict, but you can ignore it (from the iphone Json POST request to the Django server creates a QueryDict in a QueryDict ):

 def view_example(request): data=simplejson.loads(request.raw_post_data) 
+5
source

Have you tried the QueryDict.lists () or QueryDict.dict () methods? https://docs.djangoproject.com/en/dev/ref/request-response/#querydict-objects

+4
source

You can try http://code.google.com/p/jquery-json/ and make a client side json string.

 var languages = {}; languages['english'] = ['mark', 'james']; languages['spanish'] = ['amy', 'john']; var json_languages = $.toJSON(languages);//'{"plugin":"jquery-json","version":2.2}' // '{"spanish": ["amy", "john"], "english": ["mark", "james"]}' $.post('/save/', {data: json_languages}); 

only:

 if request.is_ajax() and request.method == 'POST': data = request.POST.get('languages') 

this is not the best practice, but sometimes it works great.

+1
source

All Articles