开发者

Django choicefield iniital value problem

开发者 https://www.devze.com 2023-02-14 00:50 出处:网络
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]

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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号