`required: True` set on a nullable field which is included in combined_unique
Author: tiholicCreated Dec 4, 2021Updated Aug 7, 2026
LabelsNeeds design decision
Checklist
- Raised initially as discussion #...
- This cannot be dealt with as a third party library. (We prefer new functionality to be in the form of third party libraries where possible.)
- I have reduced the issue to the simplest possible case.
My Model:
class Model:
f1 = models.CharField()
f2 = models.CharField()
f3 = models.CharField(null=True, blank=True)
class Meta:
unique_together = ('f1', 'f2', 'f3')This definition implies f1 and f2 should combinedly be unique when f3 is null, but ModelSerializer.get_uniqueness_extra_kwargs marks all fields in unique_together as required: True
Suggestion:
Ignore marking the fields as required: True when there are more than 2 fields in unique_together
Temporary fix
adding default=None on f3 currently bypasses adding required: True on it
Source: encode/django-rest-framework