Closed as not planned
Closed as not planned
Description
Checklist
- I have verified that that issue exists against the
master
branch of Django REST framework. - I have searched for similar issues in both open and closed tickets and cannot find a duplicate.
- This is not a usage question. (Those should be directed to the discussion group instead.)
- 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.
- I have included a failing test as a pull request. (If you are unable to do so we can still accept the issue.)
Steps to reproduce
- Create a model with an unique field
- Create a serializer for the model
- Instantiate the serializer with
many=True
and adata
argument containing items with non unique values for the unique field - Call
.is_valid()
method - it will returnTrue
- Call
.save()
method - will raiseIntegrityError
Example
class UniquenessModel(models.Model):
username = models.CharField(unique=True, max_length=100)
class UniquenessSerializer(serializers.ModelSerializer):
class Meta:
model = UniquenessModel
fields = '__all__'
data = [{'username': 'non-existing'}, {'username': 'non-existing'}]
serializer = UniquenessSerializer(data=data, many=True)
serializer.is_valid() # True
serializer.save() # django.db.utils.IntegrityError: UNIQUE constraint failed: tests_uniquenessmodel.username
Expected behavior
The serializer should validate the uniqueness between data items and the existing database entries as well as between themselves.
Actual behavior
The serializer only validates uniqueness between individual data items and database entries.