From a1322ca4419b6ce9c5eaedd2ad540d5e3aa8861b Mon Sep 17 00:00:00 2001 From: Michael K Date: Mon, 22 Jan 2018 16:11:56 +0100 Subject: [PATCH] Prevent validator's context to be used by another serializer Fixes encode/django-rest-framework#5760 --- rest_framework/fields.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/rest_framework/fields.py b/rest_framework/fields.py index a336528e8..40d94ecdd 100644 --- a/rest_framework/fields.py +++ b/rest_framework/fields.py @@ -251,6 +251,7 @@ class CreateOnlyDefault(object): def set_context(self, serializer_field): self.is_update = serializer_field.parent.instance is not None if callable(self.default) and hasattr(self.default, 'set_context') and not self.is_update: + self.default = copy.deepcopy(self.default) self.default.set_context(serializer_field) def __call__(self): @@ -475,6 +476,7 @@ class Field(object): raise SkipField() if callable(self.default): if hasattr(self.default, 'set_context'): + self.default = copy.deepcopy(self.default) self.default.set_context(self) return self.default() return self.default @@ -532,6 +534,7 @@ class Field(object): errors = [] for validator in self.validators: if hasattr(validator, 'set_context'): + validator = copy.deepcopy(validator) validator.set_context(self) try: