Django Rest Framework serialization guide with nested serializers for updating related models
I'm integrating two systems and I've been banging my head against this for hours. I'm working with a question when trying to update a nested serializer in Django Rest Framework (DRF). I have a `Book` model that has a foreign key relationship to an `Author` model. I want to update both the book details and its associated author information through a single API call. However, when I try to send a PATCH request with nested data, I'm getting an behavior saying `{'author': {'name': ['This field is required.']}}` even though I am including the author data in the request body. Hereβs how my serializers look like: ```python class AuthorSerializer(serializers.ModelSerializer): class Meta: model = Author fields = ['id', 'name'] class BookSerializer(serializers.ModelSerializer): author = AuthorSerializer(required=True) class Meta: model = Book fields = ['id', 'title', 'author'] def update(self, instance, validated_data): author_data = validated_data.pop('author') instance.title = validated_data.get('title', instance.title) instance.save() author, created = Author.objects.update_or_create( id=author_data['id'], defaults=author_data ) instance.author = author instance.save() return instance ``` And here's the request I'm sending: ```json { "title": "New Book Title", "author": { "id": 1, "name": "Updated Author Name" } } ``` Iβve ensured that the `id` field for the author is present, but it still raises that validation behavior. Iβve also tried removing the `required=True` from the `author` field in the `BookSerializer`, but that leads to the author not being updated at all. I'm using Django 3.2 and Django Rest Framework 3.12. Can anyone guide to understand what I'm doing wrong or how to correctly handle nested updates for this case? What am I doing wrong? Could this be a known issue?