RoR
RoR

Reputation: 1

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)

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?

Upvotes: 0

Views: 4577

Answers (1)

jammon
jammon

Reputation: 3464

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]})

Upvotes: 3

Related Questions