Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2012/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 无法在Django中将字段设置为可选字段_Python_Django - Fatal编程技术网

Python 无法在Django中将字段设置为可选字段

Python 无法在Django中将字段设置为可选字段,python,django,Python,Django,我已经更新了我的地址模型,通过添加blank=True和null=True使“State”字段成为可选字段。我现在有以下资料: state = models.CharField(max_length=12, blank=True, null=True) 我也为此运行了迁移,但在提交表单时仍出现以下错误: {"errors": {"state": "This field is required."}} 在其他什么地方它会被标记为必填字

我已经更新了我的地址模型,通过添加
blank=True
null=True
使“State”字段成为可选字段。我现在有以下资料:

state = models.CharField(max_length=12, blank=True, null=True)
我也为此运行了迁移,但在提交表单时仍出现以下错误:

{"errors": {"state": "This field is required."}}
在其他什么地方它会被标记为必填字段

地址表单包含以下内容,我是否需要删除状态引用或其他内容

class AddressForm(ModelForm):
    class Meta:
        model = Address
        exclude = ('customer',)

    def clean_zipcode(self):
        z = self.cleaned_data['zipcode']
        return z

    def clean_city(self):
        city = self.cleaned_data['city']
        return city

    def clean_state(self):
        state = self.cleaned_data['state']
        return state

迁移并不重要。表单/序列化程序是什么样子?@WillemVanOnsem我已经在表单上添加了更多细节。为什么要编写
clean_zipcode
,等等。如果不执行任何清理?@WillemVanOnsem这是我继承的代码,我还没有清理。如果
返回self.cleaned_data.get('state')
,该怎么办?