Code

Ticket #10184: querydict-pickle.diff

File querydict-pickle.diff, 2.1 KB (added by Alex, 5 years ago)
Line 
1diff --git a/django/http/__init__.py b/django/http/__init__.py
2index 60b6d15..033a0c1 100644
3--- a/django/http/__init__.py
4+++ b/django/http/__init__.py
5@@ -190,6 +190,17 @@ class QueryDict(MultiValueDict):
6             dict.__setitem__(result, copy.deepcopy(key, memo), copy.deepcopy(value, memo))
7         return result
8 
9+    def __getstate__(self):
10+        obj_dict = super(QueryDict, self).__getstate__()
11+        obj_dict['_mutable'] = self._mutable
12+        return obj_dict
13+
14+    def __setstate__(self, obj_dict):
15+        data = obj_dict.pop('_data', [])
16+        for k, v in data.items():
17+            self.setlist(k, v)
18+        self.__dict__.update(obj_dict)
19+       
20     def setlist(self, key, list_):
21         self._assert_mutable()
22         key = str_to_unicode(key, self.encoding)
23diff --git a/django/utils/datastructures.py b/django/utils/datastructures.py
24index 5837c3d..d88e7b0 100644
25--- a/django/utils/datastructures.py
26+++ b/django/utils/datastructures.py
27@@ -222,7 +222,18 @@ class MultiValueDict(dict):
28             dict.__setitem__(result, copy.deepcopy(key, memo),
29                              copy.deepcopy(value, memo))
30         return result
31-
32+   
33+    def __getstate__(self):
34+        obj_dict = self.__dict__.copy()
35+        obj_dict['_data'] = dict([(k, self.getlist(k)) for k in self])
36+        return obj_dict
37+   
38+    def __setstate__(self, obj_dict):
39+        data = obj_dict.pop('_data', [])
40+        for k, v in data.items():
41+            self.setlist(k, v)
42+        self.__dict__.update(obj_dict)
43+       
44     def get(self, key, default=None):
45         """
46         Returns the last data value for the passed key. If key doesn't exist
47diff --git a/tests/regressiontests/httpwrappers/tests.py b/tests/regressiontests/httpwrappers/tests.py
48index 844b356..49b104b 100644
49--- a/tests/regressiontests/httpwrappers/tests.py
50+++ b/tests/regressiontests/httpwrappers/tests.py
51@@ -400,6 +400,10 @@ u'\ufffd'
52 >>> q1 = pickle.loads(pickle.dumps(q, 2))
53 >>> q == q1
54 True
55+>>> q = QueryDict('a=b&c=d&a=1')
56+>>> q1 = pickle.loads(pickle.dumps(q, 2))
57+>>> q == q1
58+True
59 
60 ######################################
61 # HttpResponse with Unicode headers  #