Commit fd7db776 by Tom Christie

Bring UniqueValidator implementation in line with other uniquness validators.

parent 8c1fa0b8
...@@ -16,9 +16,9 @@ Validation in Django REST framework serializers is handled a little differently ...@@ -16,9 +16,9 @@ Validation in Django REST framework serializers is handled a little differently
With `ModelForm` the validation is performed partially on the form, and partially on the model instance. With REST framework the validation is performed entirely on the serializer class. This is advantageous for the following reasons: With `ModelForm` the validation is performed partially on the form, and partially on the model instance. With REST framework the validation is performed entirely on the serializer class. This is advantageous for the following reasons:
* It introduces a proper separation of concerns, making your code behaviour more obvious. * It introduces a proper separation of concerns, making your code behavior more obvious.
* It is easy to switch between using shortcut `ModelSerializer` classes and using explicit `Serializer` classes. Any validation behaviour being used for `ModelSerializer` is simple to replicate. * It is easy to switch between using shortcut `ModelSerializer` classes and using explicit `Serializer` classes. Any validation behavior being used for `ModelSerializer` is simple to replicate.
* Printing the `repr` of a serializer instance will show you exactly what validation rules it applies. There's no extra hidden validation behaviour being called on the model instance. * Printing the `repr` of a serializer instance will show you exactly what validation rules it applies. There's no extra hidden validation behavior being called on the model instance.
When you're using `ModelSerializer` all of this is handled automatically for you. If you want to drop down to using a `Serializer` classes instead, then you need to define the validation rules explicitly. When you're using `ModelSerializer` all of this is handled automatically for you. If you want to drop down to using a `Serializer` classes instead, then you need to define the validation rules explicitly.
......
...@@ -35,12 +35,26 @@ class UniqueValidator: ...@@ -35,12 +35,26 @@ class UniqueValidator:
# Determine the existing instance, if this is an update operation. # Determine the existing instance, if this is an update operation.
self.instance = getattr(serializer_field.parent, 'instance', None) self.instance = getattr(serializer_field.parent, 'instance', None)
def __call__(self, value): def filter_queryset(self, value, queryset):
# Ensure uniqueness. """
Filter the queryset to all instances matching the given attribute.
"""
filter_kwargs = {self.field_name: value} filter_kwargs = {self.field_name: value}
queryset = self.queryset.filter(**filter_kwargs) return queryset.filter(**filter_kwargs)
def exclude_current_instance(self, queryset):
"""
If an instance is being updated, then do not include
that instance itself as a uniqueness conflict.
"""
if self.instance is not None: if self.instance is not None:
queryset = queryset.exclude(pk=self.instance.pk) return queryset.exclude(pk=self.instance.pk)
return queryset
def __call__(self, value):
queryset = self.queryset
queryset = self.filter_queryset(value, queryset)
queryset = self.exclude_current_instance(queryset)
if queryset.exists(): if queryset.exists():
raise ValidationError(self.message) raise ValidationError(self.message)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment