开发者

Access Hidden Inputs in Django forms class

Need help accessing Hidden inputs in a Form clean_data or clean function. Unable to find solution here and Django docs after about 1 hr of searching.

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

Above gives a unicode error. I also tried:

%self.fields['action'] 


So you are trying to access action field in method for cleaning a_company_name? You don't have access to other field in field's clean method. You should use form's clean method.

From django docs:

The Form subclass’s clean() method. This method can perform any validation that requires access to multiple fields from the form at once. This is where you might put in things to check that if field A is supplied, field B must contain a valid email address and the like.

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜