Reputation: 509
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
]
}
]
Upvotes: 0
Views: 635
Reputation: 74
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.
Upvotes: 1