Django ChoiceField初始值未设置为所有ChoiceFields

时间:2013-10-10 00:03:22

标签: django django-models choicefield formfield

我有这个非常奇怪的问题,并且无法解决这个问题。我有一个带有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只是在第一个选项上设置。

我已经检查了作为初始值投入的值,这一切都符合选择......

有什么想法吗?

1 个答案:

答案 0 :(得分:1)

找出问题所在及其在数据库中的问题。

所有包类型的末尾都有一些空字符。

现在进入那个谜团。