mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-08-05 13:00:12 +03:00
Overriding Field.__deepcopy__ for RegexField
Compiled regex pattern cannot be deepcopied. The overridden __deepcopy__ mimics the behavior as the parent method, but checks for `regex` instead of `validators`
This commit is contained in:
parent
33c4278ec8
commit
26eb7c786b
|
@ -612,6 +612,18 @@ class RegexField(CharField):
|
||||||
validator = RegexValidator(regex, message=self.error_messages['invalid'])
|
validator = RegexValidator(regex, message=self.error_messages['invalid'])
|
||||||
self.validators.append(validator)
|
self.validators.append(validator)
|
||||||
|
|
||||||
|
def __deepcopy__(self, memo):
|
||||||
|
# Handling case when regex is not passed in kwargs.
|
||||||
|
args = self._args[:1] + copy.deepcopy(self._args[1:])
|
||||||
|
kwargs = dict(self._kwargs)
|
||||||
|
|
||||||
|
# deepcopy doesn't work on compiled regex pattern
|
||||||
|
regex = kwargs.pop('regex', None)
|
||||||
|
kwargs = copy.deepcopy(kwargs)
|
||||||
|
if regex is not None:
|
||||||
|
kwargs['regex'] = regex
|
||||||
|
return self.__class__(*args, **kwargs)
|
||||||
|
|
||||||
|
|
||||||
class SlugField(CharField):
|
class SlugField(CharField):
|
||||||
default_error_messages = {
|
default_error_messages = {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user