MongoEngine and WTForms

flask-mongoengine automatically generates WTForms from MongoEngine models:

from flask_mongoengine.wtf import model_form

class User(db.Document):
    email = db.StringField(required=True)
    first_name = db.StringField(max_length=50)
    last_name = db.StringField(max_length=50)

class Content(db.EmbeddedDocument):
    text = db.StringField()
    lang = db.StringField(max_length=3)

class Post(db.Document):
    title = db.StringField(max_length=120, required=True,
        validators=[validators.InputRequired(message='Missing title.')])
    author = db.ReferenceField(User)
    tags = db.ListField(db.StringField(max_length=30))
    content = db.EmbeddedDocumentField(Content)

PostForm = model_form(Post)

def add_post(request):
    form = PostForm(request.POST)
    if request.method == 'POST' and form.validate():
        # do something
        redirect('done')
    return render_template('add_post.html', form=form)

For each MongoEngine field, the most appropriate WTForm field is used. Parameters allow the user to provide hints if the conversion is not implicit:

PostForm = model_form(Post, field_args={'title': {'textarea': True}})

Supported parameters:

For fields with choices:

For StringField:

For ListField:

(By default, a StringField is converted into a TextAreaField if and only if it has no max_length.)

Supported fields

Not currently supported field types: