Suppose the following model and serializer are given.
class Human(models.Model):
name = models.TextField(unique=True)
class HumanSer(serializers.ModelSerializer):
class Meta:
model = Human
fields = '__all__'
I would like to validate that when a list of names is given, the names in that list do not repeat, without hitting the database (more on this below).
data = [{'name': 'John}, {'name': 'John'}]
ser = HumanSer(data=data, many=True)
In a perfect world I would add validate_name method, access all previously validated entries, and raise `ValidationError.
This doesn't seem possible at the moment, so the options I have found are:
- Access
.initial_dataand do the comparison. This seems not very reliable since I have to be careful, because I am working with the raw unvalidated data; pay attention to whether the data is a list or not, etc. - Do nothing, in which case
.is_valid()returnsTrue, but.save()throws an exception. Is this case the REST call fails as a whole and I can't point to the user which name is the duplicate.