Skip to content

null=True is not respected by AutoSchema when the field is on read_only_fields #8041

@canassa

Description

@canassa

Hello,

Take this ModelSerializer for example:

class MyModel(models.Model):
    my_field = models.DateTimeField(null=True, blank=True)

class MyModelSerializer(serializers.ModelSerializer):
    class Meta:
        model = MyModel
        fields = ["my_field"]
        read_only_fields = ["my_field"]

If you generate this serializer schema you will notice that the null=True was ignored by the generated schema

>>> inspector = AutoSchema()
>>> schema = inspector.map_serializer(MyModelSerializer())
>>> schema['properties']['my_field']

{'type': 'string', 'format': 'date-time', 'readOnly': True}

Removing the from the read_only_fields fixes the problem and the schema goes back to:

{'type': 'string', 'format': 'date-time', 'nullable': True}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions