I'm trying get a grip working around Django and Python. I've always been puzzled with what the self
parameter in functions is.
Example:
class RegistrationForm(forms.ModelForm):
email = forms.EmailField(label='Your Email')
password1 = forms.CharField(label='Password', \
widget=forms.PasswordInput())
password2 = forms.CharField(label='Password Confirmation', \
widget=forms.PasswordInput())
class Meta:
model = User
fields = ['username', 'email']
def clean_password2(self):
print "Inside clean_password2:"
print self
print "_________________________________________________"
password1 = self.cleaned_data.get('password1')
password2 = self.cleaned_data.get('password2')
if password1 and password2 and password1 != password2:
raise forms.ValidationError("Passwords do not match")
return password2
The output in the terminal is:
[02/Jul/2015 15:03:26]"GET /accounts/register/ HTTP/1.1" 200 5118
Inside clean_password2:
<tr><th><label for="id_username">Username:</label></th><td><input id="id_username" maxlength="30" name="username" type="text" value="TestUser" /><br /><span class="helptext">Required. 30 characters or fewer. Letters, digits and @/./+/-/_ only.</span></td></tr>
<tr><th><label for="id_email">Your Email:</label></th><td><input id="id_email" name="email" type="email" value="test@test.com" /></td></tr>
<tr><th><label for="id_password1">Password:</label></th><td><input id="id_password1" name="password1" type="password" /></td></tr>
<tr><th><label for="id_password2">Password Confirmation:</label></th><td><input id="id_password2" name="password2" type="password" /></td></tr>
Which is to say, self
attribute is whatever displayed above. Question is, Where is this function and the self attribute getting this from?
I'm not asking about the role of self
as a language feature of python. My question is squarely on how self
gets an instance in Django.
The repository of which this code a part of is here.
TL;DR: Why is all that HTML printed when I print self. Where is that coming from?