我有一个 Django 模型,其 customer_code
字段设置为唯一,但只有部分用户会为此字段分配值。其他用户只需使用此代码即可找到提供其代码作为引用编号的用户。但是,当他们提交表单时,由于该字段设置为唯一,因此会引发错误。
我想在验证后删除此错误。用户在保存前不会使用设置为 None
的值进行保存。到目前为止,我已经尝试使用表单上的自定义 clean()
方法来执行此操作:
def clean(self):
super(EmployeeForm, self).clean()
if 'customer_code' in self.errors:
del self._errors['customer_code']
return self
但这并没有奏效。感谢所有帮助,谢谢。
最佳答案
在方法的最后你应该返回cleaned_data
cleaned_data = super(EmployeeForm, self).clean()
...
return cleaned_data
关于python - Django 删除唯一字段的表单错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26701758/