1. Francisco Souza
  2. wtforms


wtforms / docs / ext.rst


WTForms ships with a number of extensions that make it easier to work with other frameworks and libraries, such as Django.


WTForms now includes support for AppEngine fields as well as auto-form generation.

Model Forms

See the module docstring for examples on how to use :func:`model_form`.

Datastore-backed Fields


For better date-time parsing using the python-dateutil package, :mod:`wtforms.ext.dateutil` provides a set of fields to use to accept a wider range of date input.


This extension provides templatetags to make it easier to work with Django templates and WTForms' html attribute rendering. It also provides a generator for automatically creating forms based on Django ORM models.


Django templates does not allow arbitrarily calling functions with parameters, making it impossible to use the html attribute rendering feature of WTForms. To alleviate this, we provide a templatetag.

Adding :mod:`wtforms.ext.django` to your INSTALLED_APPS will make the wtforms template library available to your application. With this you can pass extra attributes to form fields similar to the usage in jinja:

{% load wtforms %}

{% form_field form.username class="big_text" onclick="do_something()" %}

Note By default, using the {{ form.field }} syntax in django models will be auto-escaped. To avoid this happening, use Django's {% autoescape off %} block tag or use WTForms' form_field template tag.

Model forms

ORM-backed fields

While linking data to most fields is fairly easy, making drop-down select lists using django ORM data can be quite repetetive. To this end, we have added some helpful tools to use the django ORM along with wtforms


This extension provides SelectField integration with SQLAlchemy ORM models, similar to those in the Django extension.

ORM-backed fields

These fields are provided to make it easier to use data from ORM objects in your forms.

def enabled_categories():
    return Category.query.filter_by(enabled=True)

class BlogPostEdit(Form):
    title    = TextField()
    blog     = QuerySelectField(get_label='title')
    category = QuerySelectField(query_factory=enabled_categories, allow_blank=True)

def edit_blog_post(request, id):
    post = Post.query.get(id)
    form = ArticleEdit(obj=post)
    # Since we didn't provide a query_factory for the 'blog' field, we need
    # to set a dynamic one in the view.
    form.blog.query = Blog.query.filter(Blog.author == request.user).order_by(Blog.name)