Skip to content

@MechanisM /forms.py secret forked from mnazim/forms.py

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Make Django forms.ChoiceField accept choices from a very large data set(see: http://blog.ikraftsoft.com/post/1342312823/)
from django import forms
list_of_choices = (
(1, 'Choice 1'),
(2, 'Choice 2'),
(3, 'Choice 3'),
# ...
(n, 'Choice n'),
)
class MyForm(forms.Form):
my_choice_field = forms.ChoiceField(choices=list_of_choices)
from django import forms
class LazyChoiceField(forms.ChoiceField):
def __init__(self, choices=(), required=True, widget=None, label=None,
initial=None, help_text=None, *args, **kwargs):
super(LazyChoiceField, self).__init__(required=required, widget=widget,
label=label, initial=initial,
help_text=help_text, *args, **kwargs)
def valid_value(self, value):
# your custom validation code for "value" argument goes here
# return True if value is valid else return False
list_of_choices = (
(1, 'Choice 1'),
(2, 'Choice 2'),
(3, 'Choice 3'),
# ...
(n, 'Choice n'),
)
class MyForm(forms.Form):
my_choice_field = LazyChoiceField(choices=list_of_choices)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.