1. Marat Khabibullin
  2. wtforms

Wiki

Clone wiki

wtforms / Home

WTForms with mongoengine

This repo is fork of WTForms. I add wtforms.ext.mongoengine module.

Using

You can use WTForms like:

from wtforms.ext.mongoengine.orm import model_form
from mongoengine import *

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

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

class Post(Document):
    title = StringField(max_length=120, required=True)
    author = ReferenceField(User)
    tags = ListField(StringField(max_length=30))
    content = 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_response('add_post.html', form=form)

Supported fields

Now supported mongoengine field types:

  • StringField
  • BinaryField
  • URLField
  • EmailField
  • IntField
  • FloatField
  • DecimalField
  • BooleanField
  • DateTimeField
  • ListField (using wtforms.fields.FieldList )
  • SortedListField (dublicate ListField)
  • EmbeddedDocumentField (using wtforms.fields.FormField and generating inline Form)
  • ReferenceField (using wtforms.fields.SelectFieldBase with options loaded from QuerySet or Document)
  • DictField

Don`t supported field types:

  • ObjectIdField
  • GeoLocationField
  • GenericReferenceField

Updated