My Question - How to create Custom Text Field within the django/contrib/auth/forms.py. ?
Am trying to tweak the Django default User Model . Am adding a test field by the name "bio"
My code so far in the /python3.6/site-packages/django/contrib/auth/forms.py
file is as below -
class UserCreationForm(forms.ModelForm):
"""
A form that creates a user, with no privileges, from the given username and
password.
"""
error_messages = {
'password_mismatch': _("The two password fields didn't match."),
}
password1 = forms.CharField(
label=_("Password"),
strip=False,
widget=forms.PasswordInput,
help_text=password_validation.password_validators_help_text_html(),
)
password2 = forms.CharField(
label=_("Password confirmation"),
widget=forms.PasswordInput,
strip=False,
help_text=_("Enter the same password as before, for verification."),
)
bio = forms.CharField( #FOO_bio # this value --not getting saved in PSQL
label=_("bio_test within Forms.py"),
#widget=forms.PasswordInput, #Didnt work thus switched to forms.TextInput
widget=forms.TextInput,
strip=False,
help_text=_("Enter some dummy BIO here ."),
)
Further down in this file within the defined method - def clean_password2(self)
, am trying to add the "bio" as
def clean_password2(self):
password1 = self.cleaned_data.get("password1")
password2 = self.cleaned_data.get("password2")
bio = self.cleaned_data.get("bio") # I have no clue how to do this ?
print(bio) #prints None in the terminal
I do understand there is no key by the name "bio" within the DICT - cleaned_data.
In the file - /site-packages/django/contrib/auth/models.py
, have added the "bio" as a models.TextField
, within the class class AbstractUser(AbstractBaseUser, PermissionsMixin):
bio = models.TextField(
_('bio'),
max_length=500, blank=True)
The custom field "bio" shows up in the Registration form , the test user enters a text value - the form gets submitted , but nothing gets saved in the psql for "bio". A new user is registered and can be seen both in - psql at the terminal and within the Django Admin .
Also within the Django admin - when i go to the URL - http://digitalcognition.co.in/admin/auth/user/16/change/ , i can see a Text Field named "Bio" within the "Personal Info" section , but that again is blank . The "Email Address" , "UserName" and "Password" are seen as usual .