From 26eb7c786b651fcac96909c18817313bb4dc3306 Mon Sep 17 00:00:00 2001 From: Kevin Chang Date: Fri, 27 Feb 2015 13:51:14 -0800 Subject: [PATCH] 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` --- rest_framework/fields.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/rest_framework/fields.py b/rest_framework/fields.py index 13301f31b4..5def05146c 100644 --- a/rest_framework/fields.py +++ b/rest_framework/fields.py @@ -612,6 +612,18 @@ def __init__(self, regex, **kwargs): validator = RegexValidator(regex, message=self.error_messages['invalid']) 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): default_error_messages = { pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy