WTF module API

This is the flask_mongoengine.wtf modules API documentation.

flask_mongoengine.wtf.fields module

Useful form fields for use with the mongoengine.

class flask_mongoengine.wtf.fields.BinaryField(*args, **kwargs)[source]

Bases: TextAreaField

Custom TextAreaField that converts its value with binary_type.

process_formdata(valuelist)[source]

Process data received over the wire from a form.

This will be called during form construction with data supplied through the formdata argument.

Parameters

valuelist – A list of strings to process.

class flask_mongoengine.wtf.fields.DictField(*args, **kwargs)[source]

Bases: JSONField

Special version fo JSONField to be generated for flask_mongoengine.db_fields.DictField.

Used in generator before flask_mongoengine version 2.0

process_formdata(valuelist)[source]

Process data received over the wire from a form.

This will be called during form construction with data supplied through the formdata argument.

Parameters

valuelist – A list of strings to process.

class flask_mongoengine.wtf.fields.EmptyStringIsNoneMixin[source]

Bases: object

Special mixin to ignore empty strings before parent class processing.

Unlike old NoneStringField we do it before parent class call, this allows us to reuse this mixin in many more cases without errors.

process_formdata(valuelist)[source]

Ignores empty string and calls parent process_formdata() if data present.

Parameters

valuelist – A list of strings to process.

class flask_mongoengine.wtf.fields.JSONField(*args, **kwargs)[source]

Bases: TextAreaField

Special version fo wtforms.fields.TextAreaField.

_value()[source]
process_formdata(valuelist)[source]

Process data received over the wire from a form.

This will be called during form construction with data supplied through the formdata argument.

Parameters

valuelist – A list of strings to process.

class flask_mongoengine.wtf.fields.ModelSelectField(*args, **kwargs)[source]

Bases: QuerySetSelectField

Like a QuerySetSelectField, except takes a model class instead of a queryset and lists everything in it.

__init__(label='', validators=None, model=None, **kwargs)[source]

Init docstring placeholder.

class flask_mongoengine.wtf.fields.ModelSelectMultipleField(*args, **kwargs)[source]

Bases: QuerySetSelectMultipleField

Allows multiple select

__init__(label='', validators=None, model=None, **kwargs)[source]

Init docstring placeholder.

class flask_mongoengine.wtf.fields.MongoBooleanField(*args, **kwargs)[source]

Bases: SelectField

Mongo SelectField field for BooleanFields, that correctly coerce values.

__init__(label=None, validators=None, coerce=None, choices=None, validate_choice=True, **kwargs)[source]

Replaces defaults of wtforms.fields.SelectField with for Boolean values.

Fully compatible with wtforms.fields.SelectField and have same parameters.

class flask_mongoengine.wtf.fields.MongoDictField(json_encoder=None, json_encoder_kwargs=None, json_decoder=None, json_decoder_kwargs=None, null=None, *args, **kwargs)[source]

Bases: MongoTextAreaField

Form field to handle JSON in DictField.

__init__(json_encoder=None, json_encoder_kwargs=None, json_decoder=None, json_decoder_kwargs=None, null=None, *args, **kwargs)[source]

Special WTForms field for DictField

Configuration available with providing wtf_options on DictField initialization.

Parameters
  • json_encoder (Optional[Callable]) – Any function, capable to transform dict to string, by default json.dumps()

  • json_encoder_kwargs (Optional[dict]) – Any dictionary with parameters to json_encoder(), by default: {“indent”:4}

  • json_decoder (Optional[Callable]) – Any function, capable to transform string to dict, by default json.loads()

  • json_decoder_kwargs (Optional[dict]) – Any dictionary with parameters to json_decoder(), by default: {}

_ensure_data_is_dict()[source]

Ensures that saved data is dict, not a list or other valid parsed JSON.

_parse_json_data()[source]

Tries to load JSON data with python internal JSON library.

_value()[source]

Show existing data as pretty-formatted, or show raw data/empty dict.

process_formdata(valuelist)[source]

Process text form data to dictionary or raise JSONDecodeError.

class flask_mongoengine.wtf.fields.MongoEmailField(*args, **kwargs)[source]

Bases: EmptyStringIsNoneMixin, EmailField

Regular wtforms.fields.EmailField, that transform empty string to None.

class flask_mongoengine.wtf.fields.MongoFloatField(*args, **kwargs)[source]

Bases: FloatField

Regular wtforms.fields.FloatField, with widget replaced to wtforms.widgets.NumberInput.

widget = <wtforms.widgets.core.NumberInput object>
class flask_mongoengine.wtf.fields.MongoHiddenField(*args, **kwargs)[source]

Bases: EmptyStringIsNoneMixin, HiddenField

Regular wtforms.fields.HiddenField, that transform empty string to None.

class flask_mongoengine.wtf.fields.MongoPasswordField(*args, **kwargs)[source]

Bases: EmptyStringIsNoneMixin, PasswordField

Regular wtforms.fields.PasswordField, that transform empty string to None.

class flask_mongoengine.wtf.fields.MongoSearchField(*args, **kwargs)[source]

Bases: EmptyStringIsNoneMixin, SearchField

Regular wtforms.fields.SearchField, that transform empty string to None.

class flask_mongoengine.wtf.fields.MongoStringField(*args, **kwargs)[source]

Bases: EmptyStringIsNoneMixin, StringField

Regular wtforms.fields.StringField, that transform empty string to None.

class flask_mongoengine.wtf.fields.MongoTelField(*args, **kwargs)[source]

Bases: EmptyStringIsNoneMixin, TelField

Regular wtforms.fields.TelField, that transform empty string to None.

class flask_mongoengine.wtf.fields.MongoTextAreaField(*args, **kwargs)[source]

Bases: EmptyStringIsNoneMixin, TextAreaField

Regular wtforms.fields.TextAreaField, that transform empty string to None.

class flask_mongoengine.wtf.fields.MongoURLField(*args, **kwargs)[source]

Bases: EmptyStringIsNoneMixin, URLField

Regular wtforms.fields.URLField, that transform empty string to None.

class flask_mongoengine.wtf.fields.NoneStringField(*args, **kwargs)[source]

Bases: StringField

Custom StringField that counts “” as None

process_formdata(valuelist)[source]

Process data received over the wire from a form.

This will be called during form construction with data supplied through the formdata argument.

Parameters

valuelist – A list of strings to process.

class flask_mongoengine.wtf.fields.QuerySetSelectField(*args, **kwargs)[source]

Bases: SelectFieldBase

Given a QuerySet either at initialization or inside a view, will display a select drop-down field of choices. The data property actually will store/keep an ORM model instance, not the ID. Submitting a choice which is not in the queryset will result in a validation error.

Specifying label_attr in the constructor will use that property of the model instance for display in the list, else the model object’s __str__ or __unicode__ will be used.

If allow_blank is set to True, then a blank choice will be added to the top of the list. Selecting this choice will result in the data property being None. The label for the blank choice can be set by specifying the blank_text parameter.

__init__(label='', validators=None, queryset=None, label_attr='', allow_blank=False, blank_text='---', label_modifier=None, **kwargs)[source]

Init docstring placeholder.

_is_selected(item)[source]
iter_choices()[source]

Provides data for choice widget rendering. Must return a sequence or iterable of (value, label, selected) tuples.

pre_validate(form)[source]

Field-level validation. Runs before any other validators.

Parameters

form – The form the field belongs to.

process_formdata(valuelist)[source]

Process data received over the wire from a form.

This will be called during form construction with data supplied through the formdata argument.

Parameters

valuelist – A list of strings to process.

widget = <wtforms.widgets.core.Select object>
class flask_mongoengine.wtf.fields.QuerySetSelectMultipleField(*args, **kwargs)[source]

Bases: QuerySetSelectField

Same as QuerySetSelectField but with multiselect options.

__init__(label='', validators=None, queryset=None, label_attr='', allow_blank=False, blank_text='---', **kwargs)[source]

Init docstring placeholder.

_is_selected(item)[source]
process_formdata(valuelist)[source]

Process data received over the wire from a form.

This will be called during form construction with data supplied through the formdata argument.

Parameters

valuelist – A list of strings to process.

widget = <wtforms.widgets.core.Select object>
flask_mongoengine.wtf.fields.coerce_boolean(value)[source]

Transform SelectField boolean value from string and in reverse direction.

Return type

Optional[bool]

flask_mongoengine.wtf.models module

class flask_mongoengine.wtf.models.ModelForm(*args, **kwargs)[source]

Bases: FlaskForm

A WTForms mongoengine model form

_unbound_fields = None
_wtforms_meta = None
model_class: Type[Union[Document, DynamicDocument]]
save(commit=True, **kwargs)[source]

flask_mongoengine.wtf.orm module

Tools for generating forms based on mongoengine Document schemas.

class flask_mongoengine.wtf.orm.ModelConverter(converters=None)[source]

Bases: object

_generate_convert_base_kwargs(field, field_args)[source]
Return type

dict

classmethod _number_common(model, field, kwargs)[source]
_process_convert_for_choice_fields(field, field_class, kwargs)[source]
classmethod _string_common(model, field, kwargs)[source]
coerce(field_type)[source]
conv_Binary(model, field, kwargs)[source]
conv_Boolean(model, field, kwargs)[source]
conv_Date(model, field, kwargs)[source]
conv_DateTime(model, field, kwargs)[source]
conv_Decimal(model, field, kwargs)[source]
conv_Dict(model, field, kwargs)[source]
conv_Email(model, field, kwargs)[source]
conv_EmbeddedDocument(model, field, kwargs)[source]
conv_File(model, field, kwargs)[source]
conv_Float(model, field, kwargs)[source]
conv_GenericReference(model, field, kwargs)[source]
conv_GeoLocation(model, field, kwargs)[source]
conv_Int(model, field, kwargs)[source]
conv_List(model, field, kwargs)[source]
conv_ObjectId(model, field, kwargs)[source]
conv_Reference(model, field, kwargs)[source]
conv_SortedList(model, field, kwargs)[source]
conv_String(model, field, kwargs)[source]
conv_URL(model, field, kwargs)[source]
convert(model, field, field_args)[source]
flask_mongoengine.wtf.orm._get_fields_names(model, only, exclude)[source]

Filter fields names for further form generation.

Parameters
  • model – Source model class for fields list retrieval

  • only (Optional[List[str]]) – If provided, only these field names will have fields definition.

  • exclude (Optional[List[str]]) – If provided, field names will be excluded from fields definition. All other field names will have fields.

Return type

List[str]

flask_mongoengine.wtf.orm.converts(*args)[source]
flask_mongoengine.wtf.orm.model_fields(model, only=None, exclude=None, field_args=None, converter=None)[source]

Generate a dictionary of fields for a given database model.

See model_form() docstring for description of parameters.

Return type

OrderedDict

flask_mongoengine.wtf.orm.model_form(model, base_class=<class 'flask_mongoengine.wtf.models.ModelForm'>, only=None, exclude=None, field_args=None, converter=None)[source]

Create a wtforms Form for a given mongoengine Document schema:

from flask_mongoengine.wtf import model_form
from myproject.myapp.schemas import Article
ArticleForm = model_form(Article)
Parameters
  • model (Type[BaseDocument]) – A mongoengine Document schema class

  • base_class (Type[ModelForm]) – Base form class to extend from. Must be a ModelForm subclass.

  • only (Optional[List[str]]) – An optional iterable with the property names that should be included in the form. Only these properties will have fields. Fields are always appear in provided order, this allows user to change form fields ordering, without changing database model.

  • exclude (Optional[List[str]]) – An optional iterable with the property names that should be excluded from the form. All other properties will have fields. Fields are appears in order, defined in model, excluding provided fields names. For adjusting fields ordering, use only.

  • field_args – An optional dictionary of field names mapping to keyword arguments used to construct each field object.

  • converter – A converter to generate the fields based on the model properties. If not set, ModelConverter is used.

Return type

Type[ModelForm]

Module contents

WTFForms integration module init file.