简体   繁体   中英

How to convert raw JSON with nested array field in Postman body into form-data?

Yesterday I've asked about this question but got no response maybe because it was too specific related to Django REST Framework. I feel like it's simply the key-value pair problem in form-data I use to post. So I'm going to re-ask the question with simplified content.
What is the form-data format's equivalent for this raw JSON:

"markets": [
        {
            "market": 1,
            "name": "White Stone",
            "slabs": [
                1,
                2
            ],
            "thicknesses": [
                1,
                2,
                3
            ],
            "finish_types": [
                1
            ]
        },
        {
            "market": 2,
            "name": "White Marble",
            "slabs": [
                1
            ],
            "thicknesses": [
                1
            ],
            "finish_types": [
                1,
                3,
                6
            ]
        }
]

I want to create a new Product instance with markets field. markets is an array and has its own attributes. Some of them are also arrays. I can't send more than 1 slabs , thicknesses , and finish_types each within a single markets . slabs , thicknesses , and finish_types are foreign keys. 在此处输入图片说明 When I tried to do the key-value pairs like the image above, the only saved elements are the last one inputed.
Here's the created markets :

"markets": [
            {
                "id": 65,
                "market": 1,
                "name": "White Stone",
                "slabs": [
                    2
                ],
                "thicknesses": [
                    3
                ],
                "finish_types": [
                    1
                ]
            }
]

在此处输入图片说明 And when I tried another key format like this no slabs and thicknesses will be saved:

"markets": [
            {
                "id": 66,
                "market": 1,
                "name": "White Stone",
                "slabs": [],
                "thicknesses": [],
                "finish_types": [
                    1
                ]
            }
]

According to this answer.

How about you try this format:

Key                       Value

markets[0][market]         1
markets[0][name]           white stone
markets[0][slabs][]        2
markets[0][thicknesses][]  3
markets[0][finish_types][] 1

And maybe this Django thread might help you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM