我有这个非常奇怪的问题,一直无法弄清楚。我有一个带有 ChoiceFields 的表单,我从模型对象加载数据并将其设置为表单的初始值。只有一半的字段实际上被设置为对象指定的初始值。
继承人的形式
sku = forms.CharField(max_length=200, required=True)
weight = forms.DecimalField(min_value=.1, max_digits=4, decimal_places=1, required=True, help_text="Enter Weight In Pounds")
mail_class = forms.ChoiceField(choices=MAIL_CLASS, required=True)
package_type = forms.ChoiceField(choices=PACKAGE_TYPE, required=True)
shipping_rule = forms.IntegerField(min_value=1, required=False)
new_weight = forms.DecimalField(min_value=.1, max_digits=4, decimal_places=1, required=False, help_text="Enter Weight for new shipping method In Pounds")
new_mail_class = forms.ChoiceField(choices=MAIL_CLASS, required=False, initial='None')
new_package_type = forms.ChoiceField(choices=PACKAGE_TYPE, required=False, initial='None')
max_shipping_rule = forms.IntegerField(min_value=2, required=False)
max_weight = forms.DecimalField(min_value=.1, max_digits=4, decimal_places=1, required=False, help_text="Enter Weight for new shipping method In Pounds")
max_mail_class = forms.ChoiceField(choices=MAIL_CLASS, required=False, initial='None')
max_package_type = forms.ChoiceField(choices=PACKAGE_TYPE, required=False, initial='None')
这是我在视图中设置的
data = {
'sku': shipping_info.sku,
'weight': shipping_info.weight,
'mail_class': shipping_info.mailclass,
'package_type': shipping_info.packagetype,
'shipping_rule': shipping_info.shippingrule,
'new_weight': shipping_info.newweight,
'new_mail_class': shipping_info.newmailclass,
'new_package_type': shipping_info.newpackagetype,
'max_shipping_rule': shipping_info.maxshippingrule,
'max_weight': shipping_info.newmaxweight,
'max_mail_class': shipping_info.maxmailclass,
'max_package_type': shipping_info.maxpackagetype
}
form = ShippingType(initial=data)
唯一的问题是 mail_class 选择字段将在模板中设置为正确的值。package_type 都只是设置在第一个选项上。
我已经检查了作为初始值输入的值,它都符合选择...
有任何想法吗?