From f32ad594d17b95ca4454832b69976d4933627068 Mon Sep 17 00:00:00 2001 From: Mandeep Gill Date: Sun, 22 Dec 2019 13:08:19 +0000 Subject: [PATCH] fix: Update UniqueFieldsMixin to support DRF 3.11 validator context API --- drf_writable_nested/mixins.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/drf_writable_nested/mixins.py b/drf_writable_nested/mixins.py index 134ae7e..34d26f3 100644 --- a/drf_writable_nested/mixins.py +++ b/drf_writable_nested/mixins.py @@ -400,10 +400,13 @@ def get_fields(self): def _validate_unique_fields(self, validated_data): for field_name in self._unique_fields: unique_validator = UniqueValidator(self.Meta.model.objects.all()) - unique_validator.set_context(self.fields[field_name]) - try: - unique_validator(validated_data[field_name]) + # `set_context` removed on DRF >= 3.11, pass in via __call__ instead + if hasattr(unique_validator, 'set_context'): + unique_validator.set_context(self.fields[field_name]) + unique_validator(validated_data[field_name]) + else: + unique_validator(validated_data[field_name], self.fields[field_name]) except ValidationError as exc: raise ValidationError({field_name: exc.detail})