Skip to content

Commit fbcfb0c

Browse files
committed
to_wtf_form field_args to fields_kwargs rename, plus documentation extension
1 parent 7d6abc2 commit fbcfb0c

File tree

1 file changed

+26
-7
lines changed

1 file changed

+26
-7
lines changed

flask_mongoengine/documents.py

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""Extended version of :mod:`mongoengine.document`."""
22
import logging
3-
from typing import List, Optional, Type, Union
3+
from typing import Dict, List, Optional, Type, Union
44

55
import mongoengine
66
from flask import abort
@@ -113,7 +113,7 @@ def to_wtf_form(
113113
base_class: Type[ModelForm] = ModelForm,
114114
only: Optional[List[str]] = None,
115115
exclude: Optional[List[str]] = None,
116-
field_args=None,
116+
fields_kwargs: Optional[Dict[str, Dict]] = None,
117117
) -> Type[ModelForm]:
118118
"""
119119
Generate WTForm from Document model.
@@ -130,12 +130,31 @@ def to_wtf_form(
130130
from the form. All other properties will have fields.
131131
Fields are appears in order, defined in model, excluding provided fields
132132
names. For adjusting fields ordering, use :attr:`only`.
133-
:param field_args:
134-
An optional dictionary of field names mapping to keyword arguments used
135-
to construct each field object.
133+
:param fields_kwargs:
134+
An optional dictionary of dictionaries, where field names mapping to keyword
135+
arguments used to construct each field object. Has the highest priority over
136+
all fields settings (made in Document field definition). Field options are
137+
directly passed to field generation, so must match WTForm Field keyword
138+
arguments. Support special field keyword option ``wtf_field_class``, that
139+
can be used for complete field class replacement.
140+
141+
Dictionary format example::
142+
143+
dictionary = {
144+
"field_name":{
145+
"label":"new",
146+
"default": "new",
147+
"wtf_field_class": wtforms.fields.StringField
148+
}
149+
}
150+
151+
With such dictionary for field with name ``field_name``
152+
:class:`wtforms.fields.StringField` will be called like::
153+
154+
field_name = wtforms.fields.StringField(label="new", default="new")
136155
"""
137156
form_fields_dict = {}
138-
field_args = field_args or {}
157+
fields_kwargs = fields_kwargs or {}
139158
fields_names = cls._get_fields_names(only, exclude)
140159

141160
for field_name in fields_names:
@@ -144,7 +163,7 @@ def to_wtf_form(
144163
try:
145164
form_fields_dict[field_name] = field_class.to_wtf_field(
146165
cls,
147-
field_args.get(field_name),
166+
fields_kwargs.get(field_name),
148167
)
149168
except (AttributeError, NotImplementedError):
150169
logger.warning(

0 commit comments

Comments
 (0)