Writable nested model serializer requires manual parsing of request.data
After a lot of trouble I finally got a Django writable nested model serializer working using drf-writable-nested, for nested data with a reverse foreign key relationship. Currently this requires me to copy and manually parse request.data for all fields to create a new object before sending it to my serializer. If I don't do this I get the following (prints of serializer.data and errors respectively):
serializer.data {
"leerling": "2",
"omschrijving": "Basispakket",
"betaaltermijn": "14 dagen",
"is_credit": false,
"tegoeden": [
{
"[aantal]": "1",
"[prijs]": "450",
"[btw_tarief]": "9%"
},
{
"[aantal]": "1",
"[prijs]": "45",
"[btw_tarief]": "21%"
}
]
}
serializer.errors {
"tegoeden": [
{
"btw_tarief": [
"This field is required."
]
},
{
"btw_tarief": [
"This field is required."
]
}
]
}
Some fields don't error because they are not required/have a default value. I tried manually declaring the JSON without brackets as input for the serializer, and then it works. So the problem is caused by the brackets.
Its working now, manually parsing the request.data Querydict, but it feels hacky and probably not how it's supposed to be done. I'm probably missing something.