How to deal with nested objects via http POST request?

393 views
Skip to first unread message

han

unread,
Feb 1, 2014, 9:49:26 AM2/1/14
to django-res...@googlegroups.com
How to deal with nested objects via http POST request?

I have seen this section. So I understand how to deal with nested objects in shell.

But I don't know how to deal with nested objects in POST request...

This is my code.

And I imagine that Answer Object will be created by this way:

$ curl -X POST http://127.0.0.1:8000/questions -d "contents=How old are you?"
{"id": 1, "contents": "How old are you?"}
$ curl -X POST http://127.0.0.1:8000/answers -d "question=1&contents=13"
{"id": 1, "contents": "13"}

But it doesn't....

Creating Question object is good.

$ curl -X POST http://127.0.0.1:8000/questions -d "contents=How old are you?"
{"id": 1, "contents": "How old are you?"}

But Creating Answer object is not good.

I expected:

$ curl -X POST http://127.0.0.1:8000/answers -d "question=1&contents=13"
{"id": 1, "contents": "13"}

But actually I got:

$ curl -X POST http://127.0.0.1:8000/answers -d "question=1&contents=13" -H 'Accept: application/json; indent=4'
{
    "category": [
        "This field is required."
    ], 
    "contents": [
        "This field is required."
    ]
}

What should I do?

Thank you for reading. :)

Xavier Ordoquy

unread,
Feb 1, 2014, 10:05:55 AM2/1/14
to django-res...@googlegroups.com
Hi,

With your current serializer, you need to perform a POST for answer similar to:

{"contents": "123", "question": [{'id': 1, "contents": "How old are you?"}]}

With your POST request, it looks like you want to use the PrimaryKeyRelatedField instead of nesting question in the AnswerSerializer.

Regards,
Xavier,
Linovia.

--
You received this message because you are subscribed to the Google Groups "Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-fram...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

김한기(meoooh@gmail.com)

unread,
Feb 1, 2014, 10:23:28 AM2/1/14
to django-res...@googlegroups.com
Hi,

Thank you for replying.

Ah I need nesting question in the AnswerSerializer.

Just My mistake.

EDIT:

Actually I expected:

$ curl -X POST http://127.0.0.1:8000/answers -d "question=1&contents=13" -H 'Accept: application/json; indent=4'
{
    "question": { 
        "id": 1, 
        "contents": "How old are you?",
    },
    "id": 1, 
    "contents": "13",
}

Anyway your answer is very good!

It's good for me!!

By the way, should I use json format for requesting via http POST method?


2014-02-02 Xavier Ordoquy <xord...@linovia.com>:

--
You received this message because you are subscribed to a topic in the Google Groups "Django REST framework" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-rest-framework/EnmPxVljwJk/unsubscribe.
To unsubscribe from this group and all its topics, send an email to django-rest-fram...@googlegroups.com.

For more options, visit https://groups.google.com/groups/opt_out.



--
- 김한기(meo...@gmail.com)
Reply all
Reply to author
Forward
0 new messages