0

Django and Django Rest Framework is not sensing the array in the following JSON object:

{
   "datum":
        [
             {'proposed':'20/sep/2018', "pk":"475"},
             {'proposed':'20/sep/2018', "pk":"517"}
        ]
}

When I do a print(request.data) this is the output:

 <QueryDict: {'{"datum":[{"proposed_submission_date":"20/Sep/2018","pk":"475"},{"proposed_submission_date":"20/Sep/2018","pk":"512"}]}': ['']}>

and when I do a print(request.data.keys())I get:

 {"datum":[{"proposed_submission_date":"20/Sep/2018","pk":"475"},{"proposed_submission_date":"20/Sep/2018","pk":"512"}]}

You can see that its taking the json string as the key, and not assigning "datum" as the key.

Do I need to do something else with the JSON string?

I'm doing an AJAX PUT to the Django rest framework backend.

1 Answer 1

3

the fact that you see a QueryDict rather than just a dict is a sign that you sent your data as application/x-www-form-urlencoded or multipart/form-data.

Ensure you send the request with a application/json content type and it should be just fine.

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

1 Comment

worked like a charm when I added: contentType: "application/json" to the ajax settings. THANK YOU!!!

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.