Skip to content

Commit 3654591

Browse files
author
erdenezul
authored
Merge pull request #1584 from etng/master
[fix]validation list field with multi choice values
2 parents 0fffacc + 7fb1c9d commit 3654591

File tree

3 files changed

+29
-3
lines changed

3 files changed

+29
-3
lines changed

mongoengine/base/document.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1080,5 +1080,11 @@ def __get_field_display(self, field):
10801080
"""Return the display value for a choice field"""
10811081
value = getattr(self, field.name)
10821082
if field.choices and isinstance(field.choices[0], (list, tuple)):
1083-
return dict(field.choices).get(value, value)
1083+
if value is None:
1084+
return None
1085+
sep = getattr(field, 'display_sep', ' ')
1086+
values = value if field.__class__.__name__ in ('ListField', 'SortedListField') else [value]
1087+
return sep.join([
1088+
dict(field.choices).get(val, val)
1089+
for val in values or []])
10841090
return value

mongoengine/base/fields.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -213,8 +213,10 @@ def _validate_choices(self, value):
213213
)
214214
)
215215
# Choices which are types other than Documents
216-
elif value not in choice_list:
217-
self.error('Value must be one of %s' % six.text_type(choice_list))
216+
else:
217+
values = value if isinstance(value, (list, tuple)) else [value]
218+
if len(set(values) - set(choice_list)):
219+
self.error('Value must be one of %s' % six.text_type(choice_list))
218220

219221
def _validate(self, value, **kwargs):
220222
# Check the Choices Constraint

tests/fields/fields.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -920,6 +920,12 @@ class LogEntry(Document):
920920

921921
def test_list_validation(self):
922922
"""Ensure that a list field only accepts lists with valid elements."""
923+
AccessLevelChoices = (
924+
('a', u'Administration'),
925+
('b', u'Manager'),
926+
('c', u'Staff'),
927+
)
928+
923929
class User(Document):
924930
pass
925931

@@ -934,6 +940,7 @@ class BlogPost(Document):
934940
authors_as_lazy = ListField(LazyReferenceField(User))
935941
generic = ListField(GenericReferenceField())
936942
generic_as_lazy = ListField(GenericLazyReferenceField())
943+
access_list = ListField(choices=AccessLevelChoices, display_sep=', ')
937944

938945
User.drop_collection()
939946
BlogPost.drop_collection()
@@ -951,6 +958,17 @@ class BlogPost(Document):
951958
post.tags = ('fun', 'leisure')
952959
post.validate()
953960

961+
post.access_list = 'a,b'
962+
self.assertRaises(ValidationError, post.validate)
963+
964+
post.access_list = ['c', 'd']
965+
self.assertRaises(ValidationError, post.validate)
966+
967+
post.access_list = ['a', 'b']
968+
post.validate()
969+
970+
self.assertEqual(post.get_access_list_display(), u'Administration, Manager')
971+
954972
post.comments = ['a']
955973
self.assertRaises(ValidationError, post.validate)
956974
post.comments = 'yay'

0 commit comments

Comments
 (0)