【发布时间】:2023-04-08 10:05:02
【问题描述】:
在访问 clean_data 或 clean function 表单中的隐藏输入时需要帮助。经过大约 1 小时的搜索,无法在此处找到解决方案和 Django 文档。
class AccountForm(forms.ModelForm):
class Meta:
model = Account
#action = forms.CharField(widget=forms.HiddenInput()) <--- also tried this
exclude = [
"a_account_number"
]
# Validate that there isn't already an account that exists with a similar company name during account creation
def clean_a_company_name(self):
logging.debug("Value of action %s") % self.data.__getitem__('action')
if Account.objects.filter( a_company_name = self.cleaned_data['a_company_name']).exists() and % self.data.__getitem__('action')== 'create':
logging.debug("In account views - form validation - clean_a_company - company already exists raising exception *****************")
raise forms.ValidationError(u"An organization with this name and owner already exists. Change the Organization name or edit Organization Account information instead of creating a new Organization Account")
return self.cleaned_data["a_company_name"]
上面给出了一个 unicode 错误。我也试过了:
%self.fields['action']
【问题讨论】:
标签: django forms validation