[Python]Django動態產生選項for ChoiceField
https://stackoverflow.com/questions/22255759/django-forms-dynamic-choices-for-choicefield
e.g.
Form:
e.g.
Form:
class FooForm(forms.Form):
def __init__(self, foo_choices, *args, **kwargs):
super(FooForm, self).__init__(*args, **kwargs)
self.fields['foo'].choices = foo_choices
foo = forms.ChoiceField(choices=(), required=True)
View:...
bars = request.session['bars']
foo_list = []
for bar in bars:
foo_list.append((bar['id'], bar['name']),)
form = FooForm(foo_list)
...