wtforms / wtforms / ext / django /

Useful form fields for use with the Django ORM.
import operator
import warnings

from wtforms import widgets
from wtforms.fields import SelectFieldBase
from wtforms.validators import ValidationError

__all__ = (
    'ModelSelectField', 'QuerySetSelectField',

class QuerySetSelectField(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.

    Specify `get_label` to customize the label associated with each option. If
    a string, this is the name of an attribute on the model object to use as
    the label text. If a one-argument callable, this callable will be passed
    model instance and expected to return the label text. Otherwise, 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.
    widget = widgets.Select()

    def __init__(self, label=None, validators=None, queryset=None, get_label=None, label_attr=None, allow_blank=False, blank_text=u'', **kwargs):
        super(QuerySetSelectField, self).__init__(label, validators, **kwargs)
        self.allow_blank = allow_blank
        self.blank_text = blank_text
        if queryset is not None:
            self.queryset = queryset.all() # Make sure the queryset is fresh

        if label_attr is not None:
            warnings.warn('label_attr= will be removed in WTForms 0.7, use get_label= instead.', DeprecationWarning)
            self.get_label = operator.attrgetter(label_attr)
        elif get_label is None:
            self.get_label = lambda x: x
        elif isinstance(get_label, basestring):
            self.get_label = operator.attrgetter(get_label)
            self.get_label = get_label

    def _get_data(self):
        if self._formdata is not None:
            for obj in self.queryset:
                if == self._formdata:
        return self._data

    def _set_data(self, data):
        self._data = data
        self._formdata = None

    data = property(_get_data, _set_data)

    def iter_choices(self):
        if self.allow_blank:
            yield (u'__None', self.blank_text, is None)

        for obj in self.queryset:
            yield (, self.get_label(obj), obj ==

    def process_formdata(self, valuelist):
        if valuelist:
            if valuelist[0] == '__None':
       = None
                self._data = None
                self._formdata = int(valuelist[0])

    def pre_validate(self, form):
        if not self.allow_blank or is not None:
            for obj in self.queryset:
                if == obj:
                raise ValidationError(self.gettext('Not a valid choice'))

class ModelSelectField(QuerySetSelectField):
    Like a QuerySetSelectField, except takes a model class instead of a
    queryset and lists everything in it.
    def __init__(self, label=None, validators=None, model=None, **kwargs):
        super(ModelSelectField, self).__init__(label, validators, queryset=model._default_manager.all(), **kwargs)