1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | 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, )
|
More like this
- Choices datatype for model by menendez 5 years, 1 month ago
- caching parsed templates by forgems 5 years, 5 months ago
- Dictionary of choices based in models by marcoslhc 3 years, 1 month ago
- FieldAccessForm (per-field user access for forms derived from models) by Killarny 4 years, 7 months ago
- Limit queryset to objects related to parent in ManyToMany fields within admin inlines by DrMeers 2 years, 11 months ago
Comments