Skip to content

Commit db3d7cc

Browse files
committed
Extend WtfFieldMixin __init__ supported arguments
1 parent f821aa1 commit db3d7cc

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

flask_mongoengine/db_fields.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
]
4444
import decimal
4545
import warnings
46-
from typing import Callable, List, Optional, Union
46+
from typing import Callable, Dict, List, Optional, Type, Union
4747

4848
from bson import ObjectId
4949
from mongoengine import fields
@@ -82,17 +82,24 @@ def __init__(
8282
*,
8383
validators: Optional[Union[List, Callable]] = None,
8484
filters: Optional[Union[List, Callable]] = None,
85+
wtf_field_class: Optional[Type] = None,
8586
wtf_filters: Optional[Union[List, Callable]] = None,
8687
wtf_validators: Optional[Union[List, Callable]] = None,
88+
wtf_options: Optional[Dict] = None,
8789
**kwargs,
8890
):
8991
"""
9092
Extended :func:`__init__` method for mongoengine db field with WTForms options.
9193
9294
:param filters: DEPRECATED: wtf form field filters.
9395
:param validators: DEPRECATED: wtf form field validators.
96+
:param wtf_field_class: Any subclass of :class:`wtforms.forms.core.Field` that
97+
can be used for form field generation. Takes precedence over
98+
:attr:`DEFAULT_WTF_FIELD` and :attr:`DEFAULT_WTF_CHOICES_FIELD`
9499
:param wtf_filters: wtf form field filters.
95100
:param wtf_validators: wtf form field validators.
101+
:param wtf_options: Dictionary with WTForm Field settings.
102+
Applied last, takes precedence over any generated field options.
96103
:param kwargs: keyword arguments silently bypassed to normal mongoengine fields
97104
"""
98105
if validators is not None:
@@ -123,14 +130,29 @@ def __init__(
123130
self.wtf_filters = self._ensure_callable_or_list(
124131
wtf_filters or filters, "wtf_filters"
125132
)
133+
self.wtf_options = wtf_options
126134

127135
# Some attributes that will be updated by parent methods
128136
self.required = False
129137
self.default = None
130138
self.name = ""
139+
self.choices = None
140+
141+
# Internals
142+
self._wtf_field_class = wtf_field_class
143+
self._wtf_options = {}
131144

132145
super().__init__(**kwargs)
133146

147+
@property
148+
def wtf_field_class(self):
149+
"""Final WTForm Field class, that will be used for field generation."""
150+
if self._wtf_field_class:
151+
return self._wtf_field_class
152+
if self.choices and self.DEFAULT_WTF_CHOICES_FIELD:
153+
return self.DEFAULT_WTF_CHOICES_FIELD
154+
return self.DEFAULT_WTF_FIELD
155+
134156
@staticmethod
135157
def _ensure_callable_or_list(argument, msg_flag: str) -> Optional[List]:
136158
"""

0 commit comments

Comments
 (0)