Skip to content

Instantly share code, notes, and snippets.

View dashdrum's full-sized avatar

Dan Gentry dashdrum

View GitHub Profile
@dashdrum
dashdrum / fields.py
Last active December 13, 2015 19:08 — forked from davidbgk/fields.py
Modified from the original to mirror the functionality provided by the ModelChoiceField. 1. empty_label default is set to u"---------" 2. If there is no initial value for a required field, the empty_label is prepended. What this does is override the ChoiceField's default behavior of using the first value in the choices list if the user makes no …
from django.forms import ChoiceField
''' Based on https://gist.github.com/davidbgk/651080
modified to mirror the functionality of ModelChoiceField '''
class EmptyChoiceField(ChoiceField):
def __init__(self, choices=(), empty_label=u"---------", required=True, widget=None, label=None,
initial=None, help_text=None, *args, **kwargs):
# prepend an empty label unless the field is required AND