Django choicefield初值问题
我有一个动态选择字段:
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)
请注意,我为其设置了初始值。但是,当我单击此表单的提交按钮时,它会显示“此字段为必填字段”。当我单击选择字段并选择一个选项时,它就起作用了。但即使有初始值,我真的必须先选择吗?请帮忙?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
文档说:要指定动态初始数据,请参阅Form.初始参数。
所以这应该可以解决问题:
The documentation says: To specify dynamic initial data, see the Form.initial parameter.
So this should do the trick: