访问Django表单类中的隐藏输入

时间:2011-07-07 02:54:38

标签: django forms validation

需要帮助访问表单clean_dataclean function中的隐藏输入。在搜索约1小时后,无法在此处找到解决方案并且 Django docs

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'] 

1 个答案:

答案 0 :(得分:0)

因此,您尝试访问清除action方法中的a_company_name字段? 您无法访问字段的clean方法中的其他字段。您应该使用表单的clean方法。

来自django docs:

  

Form子类的clean()方法。   此方法可以执行任何验证   这需要访问多个   表格中的字段一次。这是   你可以把东西放在哪里检查   如果提供了字段A,则字段B   必须包含有效的电子邮件地址和   之类的。

https://docs.djangoproject.com/en/dev/ref/forms/validation/#cleaning-and-validating-fields-that-depend-on-each-other