WTForms-Appengine includes support for AppEngine fields as well as auto-form generation from models.
Note
WTForms-Appengine supports both appengine.ext.db and appengine.ext.ndb style models now, and there is some overlap between them. For the near future, we will continue to support both, but at some point will go to only supporting AppEngine for python 2.7 and drop support for ext.db models as well.
Form generation utilities for App Engine’s db.Model class.
The goal of model_form() is to provide a clean, explicit and predictable way to create forms based on db.Model classes. No malabarism or black magic should be necessary to generate a form for models, and to add custom non-model related fields: model_form() simply generates a form class that can be used as it is, or that can be extended directly or even be used to create other forms using model_form().
Example usage:
from google.appengine.ext import db
from tipfy.ext.model.form import model_form
# Define an example model and add a record.
class Contact(db.Model):
name = db.StringProperty(required=True)
city = db.StringProperty()
age = db.IntegerProperty(required=True)
is_admin = db.BooleanProperty(default=False)
new_entity = Contact(key_name='test', name='Test Name', age=17)
new_entity.put()
# Generate a form based on the model.
ContactForm = model_form(Contact)
# Get a form populated with entity data.
entity = Contact.get_by_key_name('test')
form = ContactForm(obj=entity)
Properties from the model can be excluded from the generated form, or it can include just a set of properties. For example:
# Generate a form based on the model, excluding 'city' and 'is_admin'.
ContactForm = model_form(Contact, exclude=('city', 'is_admin'))
# or...
# Generate a form based on the model, only including 'name' and 'age'.
ContactForm = model_form(Contact, only=('name', 'age'))
The form can be generated setting field arguments:
ContactForm = model_form(Contact, only=('name', 'age'), field_args={
'name': {
'label': 'Full name',
'description': 'Your name',
},
'age': {
'label': 'Age',
'validators': [validators.NumberRange(min=14, max=99)],
}
})
The class returned by model_form() can be used as a base class for forms mixing non-model fields and/or other model forms. For example:
# Generate a form based on the model.
BaseContactForm = model_form(Contact)
# Generate a form based on other model.
ExtraContactForm = model_form(MyOtherModel)
class ContactForm(BaseContactForm):
# Add an extra, non-model related field.
subscribe_to_news = f.BooleanField()
# Add the other model form as a subform.
extra = f.FormField(ExtraContactForm)
The class returned by model_form() can also extend an existing form class:
class BaseContactForm(Form):
# Add an extra, non-model related field.
subscribe_to_news = f.BooleanField()
# Generate a form based on the model.
ContactForm = model_form(Contact, base_class=BaseContactForm)
Creates and returns a dynamic wtforms.Form class for a given db.Model class. The form class can be used as it is or serve as a base for extended form classes, which can then mix non-model related fields, subforms with other model forms, among other possibilities.
Parameters: |
|
---|
A field for db.ReferenceProperty. The list items are rendered in a select.
Parameters: |
|
---|
A field for db.StringListProperty. The list items are rendered in a textarea.
A field for db.StringListProperty. The list items are rendered in a textarea.
WTForms now includes support for NDB models and can support mapping the relationship fields as well as generating forms from models.
A field for ndb.KeyProperty. The list items are rendered in a select.
Parameters: |
|
---|
Creates and returns a dynamic wtforms.Form class for a given ndb.Model class. The form class can be used as it is or serve as a base for extended form classes, which can then mix non-model related fields, subforms with other model forms, among other possibilities.
Parameters: |
|
---|