Django choicefield iniital value problem
I have a dynamic choice field:
PASSENGER_TYPE_CHOICE = [(p.id, p.cabin.name) for p in flight.passengertype_set.all() if p.availableSeats > 0]
self.fields['passenger_type'] = forms.ChoiceField(label=str(self.flight.code)+" Seat Type", initial=PASSENGER_TYPE_CHOICE[0][0],choices=PASSENGER_TYPE_CHOICE)
开发者_StackOverflow
Notice that I put an initial value for it. But when I click the submit button for this form, it raises "This field is required". When I click the choicefield and select an option, it works. But do i really have to select first, even when there is an initial value? Please help?
The documentation says: To specify dynamic initial data, see the Form.initial parameter.
So this should do the trick:
form = MyPassengerForm(initial = {'passenger_type': PASSENGER_TYPE_CHOICE[0][0]})
精彩评论