The Python Oracle

Django rest framework nested serialization not working properly

Become part of the top 3% of the developers by applying to Toptal https://topt.al/25cXVn

--

Music by Eric Matyas
https://www.soundimage.org
Track title: Techno Bleepage Open

--

Chapters
00:00 Question
02:29 Accepted answer (Score 2)
03:10 Thank you

--

Full question
https://stackoverflow.com/questions/3838...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python #django #debugging #serialization #djangorestframework

#avk47



ACCEPTED ANSWER

Score 2


If you are just sending IDs then you don't need to add the nested serializer, just specify the field name of the ForeignKey or ManyToManyField.

class WriteTeacherSerializer(serializers.ModelSerializer):

    class Meta:
        model = Teacher
        fields = ('phone_number', 'bio', 'price', 'disciplines', 'schedules')

I am also wondering if it is because you have a depth=1 flag?

DRF doesn't support nested updates out of the box. You have to override the Serializer's update method, and write your own update logic, so you'd be seeing an error for this warning if you were sending nested data.