【问题标题】:Django ChoiceField Initial Value Not Setting for All ChoiceFields未为所有 ChoiceFields 设置 Django ChoiceField 初始值
【发布时间】:2023-03-16 09:06:01
【问题描述】:

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

我已经检查了作为初始值输入的值,它都符合选择...

有什么想法吗?

【问题讨论】:

    标签: django django-models choicefield formfield


    【解决方案1】:

    找出问题所在以及数据库本身的问题。

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

    现在进入那个谜。

    【讨论】:

      猜你喜欢
      • 2011-12-13
      • 1970-01-01
      • 1970-01-01
      • 2015-01-14
      • 1970-01-01
      • 2014-07-07
      • 1970-01-01
      • 2013-08-08
      • 1970-01-01
      相关资源
      最近更新 更多