From 3173384d680526c9f018d573fdef2f83a0b12ff0 Mon Sep 17 00:00:00 2001 From: Andrew Miller Date: Tue, 25 Jul 2017 16:45:19 +0100 Subject: [PATCH 1/3] move MSFList class to global scope Fixes #65 which enables serialisation with pickle --- multiselectfield/db/fields.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/multiselectfield/db/fields.py b/multiselectfield/db/fields.py index fd050ea..1bb24ea 100644 --- a/multiselectfield/db/fields.py +++ b/multiselectfield/db/fields.py @@ -34,7 +34,6 @@ else: # Code from six egg https://bitbucket.org/gutworth/six/src/a3641cb211cc360848f1e2dd92e9ae6cd1de55dd/six.py?at=default - def add_metaclass(metaclass): """Class decorator for creating a class with a metaclass.""" def wrapper(cls): @@ -47,6 +46,13 @@ def add_metaclass(metaclass): return wrapper +@python_2_unicode_compatible +class MSFList(list): + def __str__(msgl): + l = [choices.get(int(i)) if i.isdigit() else choices.get(i) for i in msgl] + return u', '.join([string_type(s) for s in l]) + + class MultiSelectField(models.CharField): """ Choice values can not contain commas. """ @@ -131,12 +137,6 @@ class MultiSelectField(models.CharField): def to_python(self, value): choices = dict(self.flatchoices) - @python_2_unicode_compatible - class MSFList(list): - def __str__(msgl): - l = [choices.get(int(i)) if i.isdigit() else choices.get(i) for i in msgl] - return u', '.join([string_type(s) for s in l]) - if value: return value if isinstance(value, list) else MSFList(value.split(',')) return MSFList([]) From dadfdaee1248847f6ed2f5f67cd3cc2976d650e2 Mon Sep 17 00:00:00 2001 From: Andrew Miller Date: Tue, 25 Jul 2017 16:56:26 +0100 Subject: [PATCH 2/3] flake 8 --- multiselectfield/db/fields.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/multiselectfield/db/fields.py b/multiselectfield/db/fields.py index 1bb24ea..876638c 100644 --- a/multiselectfield/db/fields.py +++ b/multiselectfield/db/fields.py @@ -34,6 +34,7 @@ else: # Code from six egg https://bitbucket.org/gutworth/six/src/a3641cb211cc360848f1e2dd92e9ae6cd1de55dd/six.py?at=default + def add_metaclass(metaclass): """Class decorator for creating a class with a metaclass.""" def wrapper(cls): @@ -48,8 +49,13 @@ def add_metaclass(metaclass): @python_2_unicode_compatible class MSFList(list): + + def __init__(self, choices, *args, **kwargs): + self.choices = choices + super(MSFList, self).__init__(*args, **kwargs) + def __str__(msgl): - l = [choices.get(int(i)) if i.isdigit() else choices.get(i) for i in msgl] + l = [msgl.choices.get(int(i)) if i.isdigit() else msgl.choices.get(i) for i in msgl] return u', '.join([string_type(s) for s in l]) From d6ad0115b7f4de7bc3860e807422bde69d3cf4c6 Mon Sep 17 00:00:00 2001 From: Andrew Miller Date: Tue, 25 Jul 2017 16:58:52 +0100 Subject: [PATCH 3/3] finish flake8 and pass choices to MSFList --- multiselectfield/db/fields.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/multiselectfield/db/fields.py b/multiselectfield/db/fields.py index 876638c..57b053f 100644 --- a/multiselectfield/db/fields.py +++ b/multiselectfield/db/fields.py @@ -144,8 +144,8 @@ class MultiSelectField(models.CharField): choices = dict(self.flatchoices) if value: - return value if isinstance(value, list) else MSFList(value.split(',')) - return MSFList([]) + return value if isinstance(value, list) else MSFList(choices, value.split(',')) + return MSFList(choices, []) def from_db_value(self, value, expression, connection, context): if value is None: