Undocumented Change in Behavior Involving UniqueConstraint Fields

60 views
Skip to first unread message

Sandy Suh

unread,
Mar 27, 2025, 2:15:17 AMMar 27
to Django REST framework
So there's a change between v3.14.0 and v3.15.1 involving serializer fields that are part of a UniqueConstraint, and this change isn't documented to my knowledge. Consider the following example:

class Dummy(models.Model):
    name = models.TextField(blank=True)
    url = models.URLField()

    class Meta:
        constraints = [models.UniqueConstraint(fields=["url", "name"], name="url_name_unique")]

class DummySerializer(serializers.ModelSerializer):
    class Meta:
        model = Dummy
        fields = ("name", "url")

In DRF 3.14.0, creating a Dummy using a DummySerializer without specifying a name will work (the name will be the empty string). But in DRF 3.15.1, this will not work. You'll get the error 'This field is required.' for the name field, and looking at the codebase, this seems to be due to changes to `get_uniqueness_extra_args` in serializers.py, which make a field that is part of a UniqueConstraint required by default.

To my knowledge, this change in behavior is not documented, so I thought I'd bring it to people's attention. 

Thanks!
Reply all
Reply to author
Forward
0 new messages