I am developing an API in DRF and have stumbled upon one issue that hopefully you can help me to solve. Let's assume I have the following model and a serializer:
class SomeModelBase(models.Model):
property = models.ForeignKey(
Property,
on_delete=models.PROTECT,
)
name = models.CharField()
address = models.OneToOneField(null=True, blank=True)
class SomeModel(SomeModelBase):
...more fields
class SomeModelBaseSerializer(serializers.ModelSerializer):
class Meta:
fields = "__all__"
model = SomeModelBase
extra_kwargs = {"address": {"required": True},}
class SomeModelSerializer(SomeModelBaseSerializer):
class Meta:
model = SomeModel
fields = "__all__""
extra_kwargs = {
"name": {"required": True},
"address": {"required": False},
}
As you can see I am trying to add field kwargs by using extra_kwargs. The issue is that it won't work for a related field e.g. property or address field. I know I can just override it by defining a field manually and passing required=True, and that works but it's too long for my use case. Do you have any idea why this happen and how could i possibly modify required in each child class? Thanks.