From b7e064207c27fcdd1b97617c18c0d3331b34d2c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Honza=20Kr=C3=A1l?= Date: Wed, 5 Aug 2009 01:28:39 +0000 Subject: [PATCH] [soc2009/model-validation] remove lazily translated strings for pickling of db fields git-svn-id: http://code.djangoproject.com/svn/django/branches/soc2009/model-validation@11399 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/db/models/fields/__init__.py | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/django/db/models/fields/__init__.py b/django/db/models/fields/__init__.py index 2f974974f0..8b53cd95c3 100644 --- a/django/db/models/fields/__init__.py +++ b/django/db/models/fields/__init__.py @@ -112,6 +112,35 @@ class Field(object): messages.update(error_messages or {}) self.error_messages = messages + def __getstate__(self): + """ + Pickling support. + """ + from django.utils.functional import Promise + obj_dict = self.__dict__.copy() + items = [] + translated_keys = [] + for k, v in self.error_messages.items(): + if isinstance(v, Promise): + args = getattr(v, '_proxy____args', None) + if args: + translated_keys.append(k) + v = args[0] + items.append((k,v)) + obj_dict['_translated_keys'] = translated_keys + obj_dict['error_messages'] = dict(items) + return obj_dict + + def __setstate__(self, obj_dict): + """ + Unpickling support. + """ + translated_keys = obj_dict.pop('_translated_keys') + self.__dict__.update(obj_dict) + for k in translated_keys: + self.error_messages[k] = _(self.error_messages[k]) + + def __cmp__(self, other): # This is needed because bisect does not take a comparison function. return cmp(self.creation_counter, other.creation_counter)