I have this form class :
class MyForm(forms.Form):
def __init__(self, *args, **kwargs):
self.notvalidate = kwargs.pop('notvalidate',False)
super(MyForm, self).__init__(*args, **kwargs)
email = forms.EmailField(widget=forms.TextInput(attrs=dict(attrs_dict,maxlength=75)))
(...)
if not notvalidate:
def clean_email(self):
email = self.cleaned_data.get("email")
if email and User.objects.filter(email=email).count() > 0:
raise forms.ValidationError(
_(u"Email already used."))
return email
Although in init I set self.notvalidate value to either True(if was given) or False inside the body of MyForm I’m getting name 'notvalidate' is not defined (or if I check for self.notvalidate – name 'self' is not defined). What is wrong ?
What are you trying to achieve is changing the class level attribute
clean_emailbut you want to do that using instance attributeself.notvalidate, so you are doing contradictory things here. Simplest way to not validate would be to check in clean_email and return e.gBut if due to some mysterious reason you do not want clean_mail method to be existing in the class at all, you need to create a class using metaclass or simpler way would be to call a function to create class e.g.
Though I will strongly suggest NOT to do this.