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')
,该怎么办?