Django Rest Framework Serialization implementing Nested Relationships and Optional Fields
I'm trying to configure I tried several approaches but none seem to work... I'm currently working on a Django application using Django Rest Framework (DRF) and I'm working with an scenario with serialization of nested relationships. I have a model `Book` that has a foreign key relationship to an `Author` model. The `Book` model also includes optional fields such as `published_date` and `genre`, but these aren't mandatory when creating a new `Book` instance. Here's a simplified version of my models: ```python from django.db import models class Author(models.Model): name = models.CharField(max_length=100) class Book(models.Model): title = models.CharField(max_length=100) author = models.ForeignKey(Author, on_delete=models.CASCADE) published_date = models.DateField(null=True, blank=True) genre = models.CharField(max_length=50, null=True, blank=True) ``` I have created a serializer for the `Book` model: ```python from rest_framework import serializers class BookSerializer(serializers.ModelSerializer): class Meta: model = Book fields = ['id', 'title', 'author', 'published_date', 'genre'] ``` When I try to create a new book instance without `published_date` or `genre`, I'm getting the following behavior: ``` ValidationError: {'published_date': [ErrorDetail(string='This field may not be null.', code='null')], 'genre': [ErrorDetail(string='This field may not be null.', code='null')]} ``` I've tried setting the fields as optional in the serializer by overriding the `create` method and ensuring that these fields can be `None`, but I'm still running into this validation behavior. I've also checked that `blank=True` and `null=True` are set correctly in the model definition, but the serializer seems to be enforcing these fields as required. What can I do to allow optional fields in my serializer without working with a validation behavior? I'm working on a API that needs to handle this. Hoping someone can shed some light on this.