1

I'm attempting to send a dictionary from jQuery to Django using a getJSON call:

jQuery.getJSON(URL,JSONData,function(returnData){});

The JSONData object is formatted as follows:

JSONData = {
     year:2010101,
     name:"bob",

     data:{
          search:[jim,gordon],
          register:[jim],
          research:[dave],
          }
}

This is put together programmatically but looks fine.

Once passed to Django the "year" and "name" objects are as expected. The data object however contains the following keys/values - "search[0]":"jim", "search[1]":"gordon","register[0]":"jim","research[0]":"dave", rather than the expected "search":(array of data), "register":(array of data), "research":(array of data).

Similar things happen if I use objects in place of the arrays.

Is this an issue with Django's interpretation of the object?

Any idea how I might correct this...cleanly?

EDIT:

I have now simplified the data to make testing easier:

JSONData = { 
     year:2010101, 
     name:"bob", 
     search:[jim,gordon], 
     register:[jim], 
     research:[dave], 

} 
3
  • Can you also post how you handle the data in your view? Commented Nov 3, 2010 at 23:58
  • at this stage I'm literally returning request.GET.keys() Commented Nov 4, 2010 at 0:00
  • for now I've conceded defeat, instead of a list I'm using a string and splitting it on the django end. Commented Nov 4, 2010 at 0:42

1 Answer 1

4

request.GET is not an instance of a normal python dict, but of the django class QueryDict, that can deal with multiple values for one key. If you need multiple values for a key returned as a list you have to use getList!

EDIT: Also have a look at this jQuery parameter settings!

Sign up to request clarification or add additional context in comments.

2 Comments

Nope, the problem still remains that the data has been split up incorrectly and the key names mangled. eg request.GET.getlist("Search") should give me the associated list, instead it returns nothing. request.GET.getlist("Search[0]") gives me the single value as above.
Aha! had to set traditional = True in the parameter settings. Nice work!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.