0

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 .

Community
  • 1
  • 1
Rohit Dhankar
  • 1,574
  • 18
  • 25

1 Answers1

1

Don't modify the default User model. As mentioned in the documentation:

If you wish to store information related to User, you can use a OneToOneField to a model containing the fields for additional information. This one-to-one model is often called a profile model, as it might store non-auth related information about a site user.

And you definitely shouldn't modify django code. Just create a new form that inherits from django's UserCreationForm, and add your fields there.

See the accepted answer here.

Bober
  • 479
  • 3
  • 6
  • thanks for your answer - am already aware of these recommendations to not tweak the default User model , am doing this from a learning perspective . Unless its not possible to add new custom fields in this manner , i would like to add these n learn how it can be done - thanks again . – Rohit Dhankar Nov 06 '19 at 18:06
  • Am trying the method used in the linked answer , i need to post back my results , i have upvoted your answer - thanks – Rohit Dhankar Nov 06 '19 at 18:17
  • 1
    The key is to override your form's save() method, and make sure your data is saved to the database there. Good luck! – Bober Nov 06 '19 at 18:21