dabapps / django-readers

A lightweight function-oriented toolkit for better organisation of business logic and efficient selection and projection of data in Django projects.
https://www.django-readers.org
BSD 2-Clause "Simplified" License
186 stars 7 forks source link

Generated child serializers do not correctly set the allow_null property #81

Closed j4mie closed 1 year ago

j4mie commented 1 year ago

During serializer generation, when a relationship is nullable, the allow_null argument should be passed into the nested serializer when it's instantiated.

class Owner(models.Model):
    name = models.CharField(max_length=100)

class Widget(models.Model):
    name = models.CharField(max_length=100)
    owner = models.ForeignKey(Owner, null=True, on_delete=models.SET_NULL)

spec = [
    "name",
    {"owner": ["name"]}
]

This results in

WidgetSerializer():
    name = CharField(allow_null=True, max_length=100, read_only=True, required=False)
    owner = WidgetOwnerSerializer(read_only=True):
        name = CharField(max_length=100, read_only=True)

But should be

WidgetSerializer():
    name = CharField(allow_null=True, max_length=100, read_only=True, required=False)
    owner = WidgetOwnerSerializer(allow_null=True, read_only=True):
        name = CharField(max_length=100, read_only=True)