from django.utils.datastructures import SortedDict
class Choices(SortedDict):
def __init__(self, *choices):
super(Choices, self).__init__()
for choice in choices:
self[choice[0]] = choice[1]
@property
def choices(self):
choices = []
for key, value in self.items():
choices.append( (key, value,) )
return tuple(choices)
def __iter__(self):
return self.choices.__iter__()
# Choices implementation
class MyForm(forms.Form):
CHOICE1 = 0
CHOICE2 = 1
CHOICE3 = 2
CHOICES = Choices(
(CHOICE1, 'Choice 1'),
(CHOICE2, 'Choice 2'),
(CHOICE3, 'Choice 3'),
)
field = forms.ChoiceField(
choices=CHOICES, )
Comments