Django choicefield iniital value problem

4.5k Views Asked by At

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?

1

There are 1 best solutions below

0
On

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