diff options
| author | Tom Christie | 2014-11-05 10:48:30 +0000 |
|---|---|---|
| committer | Tom Christie | 2014-11-05 10:48:30 +0000 |
| commit | 0a5d088287be1bb56f37504cc75cee10fb4e74a0 (patch) | |
| tree | bd7a05cb9496d4a374fa4d6186c631a752c5c82b /rest_framework/fields.py | |
| parent | 26b6180f5047e965383553177cd5cd7938bbe63e (diff) | |
| download | django-rest-framework-0a5d088287be1bb56f37504cc75cee10fb4e74a0.tar.bz2 | |
Fix failing copy of fields when RegexValidator is used. Closes #1954.
Diffstat (limited to 'rest_framework/fields.py')
| -rw-r--r-- | rest_framework/fields.py | 16 |
1 files changed, 12 insertions, 4 deletions
diff --git a/rest_framework/fields.py b/rest_framework/fields.py index 24ddb7a4..363b684f 100644 --- a/rest_framework/fields.py +++ b/rest_framework/fields.py @@ -1,7 +1,7 @@ from django.conf import settings -from django.core import validators from django.core.exceptions import ObjectDoesNotExist from django.core.exceptions import ValidationError as DjangoValidationError +from django.core.validators import RegexValidator from django.forms import ImageField as DjangoImageField from django.utils import six, timezone from django.utils.datastructures import SortedDict @@ -392,7 +392,15 @@ class Field(object): originally created with, rather than copying the complete state. """ args = copy.deepcopy(self._args) - kwargs = copy.deepcopy(self._kwargs) + kwargs = dict(self._kwargs) + # Bit ugly, but we need to special case 'validators' as Django's + # RegexValidator does not support deepcopy. + # We treat validator callables as immutable objects. + # See https://github.com/tomchristie/django-rest-framework/issues/1954 + validators = kwargs.pop('validators', None) + kwargs = copy.deepcopy(kwargs) + if validators is not None: + kwargs['validators'] = validators return self.__class__(*args, **kwargs) def __repr__(self): @@ -531,7 +539,7 @@ class RegexField(CharField): def __init__(self, regex, **kwargs): super(RegexField, self).__init__(**kwargs) - validator = validators.RegexValidator(regex, message=self.error_messages['invalid']) + validator = RegexValidator(regex, message=self.error_messages['invalid']) self.validators.append(validator) @@ -543,7 +551,7 @@ class SlugField(CharField): def __init__(self, **kwargs): super(SlugField, self).__init__(**kwargs) slug_regex = re.compile(r'^[-a-zA-Z0-9_]+$') - validator = validators.RegexValidator(slug_regex, message=self.error_messages['invalid']) + validator = RegexValidator(slug_regex, message=self.error_messages['invalid']) self.validators.append(validator) |
