Skip to content

SerializerMethodField issue after "unique together validator" fix #9700

Description

@simone-gasparini

DRF version 3.16.0

After the "Fix unique together validator doesn't respect condition's fields (#9360)" merge, a SerializerMethodField named as the same of a model field goes into the HiddenField because of the unique_constraint_field is null

model.py (part)

class Repository(models.Model):
    id = models.UUIDField(primary_key=True, default=uuid.uuid4)

    prefix = models.ForeignKey(
        "RepositoryPrefix",
        related_name="repository_set",
        on_delete=CASCADE,
        blank=False,
        null=True
    )
    ...
    
    class Meta:
        unique_together = [("prefix", "name")]
        ...

serializer.py

class RepositorySerializer(serializers.ModelSerializer):
    prefix = serializers.SerializerMethodField()
    ...

    def get_prefix(self, obj):
        return obj.prefix.name if obj.prefix else None

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions