Together than removing UniqueValidator with
'name': {'validators': []}
You need to check the unique record yourself, ignoring the current object, so as not to get a 500 error when another person tries to keep the same name, something like this will work:
def validate_name(self, value): check_query = Genre.objects.filter(name=value) if self.instance: check_query = check_query.exclude(pk=self.instance.pk) if self.parent is not None and self.parent.instance is not None: genre = getattr(self.parent.instance, self.field_name) check_query = check_query.exclude(pk=genre.pk) if check_query.exists(): raise serializers.ValidationError('A Genre with this name already exists .') return value
The validate_<field> method is called to validate all of your fields, see the Documentation .
source share