Skip to content

Commit 936efc9

Browse files
committed
Added customisation hooks for FieldList sub fields (MongoEngine#19)
1 parent 10fdc56 commit 936efc9

File tree

4 files changed

+26
-2
lines changed

4 files changed

+26
-2
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+
- Added customisation hooks for FieldList sub fields (#19)
78
- Handle non ascii chars in the MongoDebugPanel (#22)
89
- Fixed toolbar stacktrace if a html directory is in the path (#31)
910
- ModelForms no longer patch Document.update (#32)

flask_mongoengine/wtf/models.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
class ModelForm(Form):
55
"""A WTForms mongoengine model form"""
6+
67
def __init__(self, formdata=None, obj=None, prefix='', **kwargs):
78
self.instance = (kwargs.pop('instance', None) or kwargs.get('obj', None))
89
if self.instance and not formdata:

flask_mongoengine/wtf/orm.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,8 +141,9 @@ def conv_List(self, model, field, kwargs):
141141
return ModelSelectMultipleField(model=field.field.document_type, **kwargs)
142142
if field.field.choices:
143143
kwargs['multiple'] = True
144-
return self.convert(model, field.field, kwargs)
145-
unbound_field = self.convert(model, field.field, {})
144+
return self.convert(model, field.field, **kwargs)
145+
sub_field_args = kwargs.pop("sub_field_args", {})
146+
unbound_field = self.convert(model, field.field, sub_field_args)
146147
unacceptable = {
147148
'validators': [],
148149
'filters': [],

tests.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,5 +284,26 @@ class Item (db.Document):
284284

285285
self.assertEqual(1, Item.objects.count())
286286

287+
def test_sub_field_args(self):
288+
with self.app.test_request_context('/'):
289+
db = self.db
290+
291+
class TestModel(db.Document):
292+
lst = db.ListField(db.StringField())
293+
294+
field_args = {'lst': {'label': 'Custom Label',
295+
'sub_field_args': {'widget': wtforms.widgets.HiddenInput(),
296+
'label': "Hidden Input"}}}
297+
CustomForm = model_form(TestModel, field_args=field_args)
298+
299+
custom_form = CustomForm(obj=TestModel(lst=["Foo"]))
300+
list_label = flask.render_template_string("{{ custom_form.lst.label }}", custom_form=custom_form)
301+
self.assertTrue("Custom Label" in list_label)
302+
self.assertTrue("Hidden Input" not in list_label)
303+
304+
sub_label = flask.render_template_string("{{ custom_form.lst }}", custom_form=custom_form)
305+
self.assertTrue("Hidden Input" in sub_label)
306+
307+
287308
if __name__ == '__main__':
288309
unittest.main()

0 commit comments

Comments
 (0)