Skip to content

Commit 18ddc80

Browse files
committed
Fixed only / exclude in model forms (#49)
1 parent d9563ed commit 18ddc80

File tree

3 files changed

+25
-3
lines changed

3 files changed

+25
-3
lines changed

docs/changelog.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ Changelog
44

55
Changes in 0.7
66
==============
7+
- Fixed only / exclude in model forms (#49)
78
- Added automatic choices coerce for simple types (#34)
89
- Fixed EmailField and URLField rendering and validation (#44, #9)
910
- Use help_text for field description (#43)

flask_mongoengine/wtf/orm.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -226,10 +226,9 @@ def model_fields(model, only=None, exclude=None, field_args=None, converter=None
226226
field_names = map(itemgetter(0), sorted(names, key=itemgetter(1)))
227227

228228
if only:
229-
field_names = set(field_names)
230-
field_names = (x for x in only if x in field_names)
229+
field_names = set((x for x in only if x in set(field_names)))
231230
elif exclude:
232-
field_names = (x for x in field_names if x not in exclude)
231+
field_names = set((x for x in set(field_names) if x not in exclude))
233232

234233
field_dict = OrderedDict()
235234
for name in field_names:

tests/test_forms.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,27 @@ class LinkPost(BlogPost):
150150

151151
self.assertEqual(post.tags, ['flask', 'mongodb', 'mongoengine', 'flask-mongoengine'])
152152

153+
def test_model_form_only(self):
154+
with self.app.test_request_context('/'):
155+
db = self.db
156+
157+
class BlogPost(db.Document):
158+
title = db.StringField(required=True, max_length=200)
159+
posted = db.DateTimeField(default=datetime.datetime.now)
160+
tags = db.ListField(db.StringField())
161+
162+
BlogPost.drop_collection()
163+
164+
BlogPostForm = model_form(BlogPost, only=['tags'])
165+
form = BlogPostForm()
166+
self.assertTrue(hasattr(form, 'tags'))
167+
self.assertFalse(hasattr(form, 'posted'))
168+
169+
BlogPostForm = model_form(BlogPost, exclude=['posted'])
170+
form = BlogPostForm()
171+
self.assertTrue(hasattr(form, 'tags'))
172+
self.assertFalse(hasattr(form, 'posted'))
173+
153174
def test_model_form_with_custom_query_set(self):
154175
with self.app.test_request_context('/'):
155176
db = self.db
@@ -335,5 +356,6 @@ class Post(db.Document):
335356
self.assertTrue("content-text" in "%s" % form.content.text)
336357

337358

359+
338360
if __name__ == '__main__':
339361
unittest.main()

0 commit comments

Comments
 (0)