diff options
| author | Carlton Gibson | 2014-03-06 22:48:16 +0100 |
|---|---|---|
| committer | Carlton Gibson | 2014-03-06 22:48:16 +0100 |
| commit | d48e8ca8d6372ae73a6b420e92c546b017fd0b46 (patch) | |
| tree | 391e321bf37e1dd257c35a4ba4e68d1b59b1f54f /rest_framework/serializers.py | |
| parent | 2090f452b224e60853b40e73d1d0e9aad58cd24a (diff) | |
| parent | 51e6982397cc032d6b3fd66f452713d448eb9084 (diff) | |
| download | django-rest-framework-d48e8ca8d6372ae73a6b420e92c546b017fd0b46.tar.bz2 | |
Merge pull request #1459 from linovia/bugfix/optional_unique_validation
Unique constraint are validated even if the field is optional.
Diffstat (limited to 'rest_framework/serializers.py')
| -rw-r--r-- | rest_framework/serializers.py | 6 |
1 files changed, 3 insertions, 3 deletions
diff --git a/rest_framework/serializers.py b/rest_framework/serializers.py index c95b0593..5c726dfc 100644 --- a/rest_framework/serializers.py +++ b/rest_framework/serializers.py @@ -881,7 +881,7 @@ class ModelSerializer(Serializer): except KeyError: return ModelField(model_field=model_field, **kwargs) - def get_validation_exclusions(self): + def get_validation_exclusions(self, instance=None): """ Return a list of field names to exclude from model validation. """ @@ -893,7 +893,7 @@ class ModelSerializer(Serializer): field_name = field.source or field_name if field_name in exclusions \ and not field.read_only \ - and field.required \ + and (field.required or hasattr(instance, field_name)) \ and not isinstance(field, Serializer): exclusions.remove(field_name) return exclusions @@ -908,7 +908,7 @@ class ModelSerializer(Serializer): the full_clean validation checking. """ try: - instance.full_clean(exclude=self.get_validation_exclusions()) + instance.full_clean(exclude=self.get_validation_exclusions(instance)) except ValidationError as err: self._errors = err.message_dict return None |
