[Django]-How to add extra fields using django.forms Textarea

13👍

Firstly, as Digitalpbk says, don’t manually add columns to Django’s tables. Instead, create a UserProfile model in your own app, with a OneToOneField to auth.User.

Secondly, to add extra fields to a modelform, you need to define them explicitly at the form level:

class UserForm(ModelForm):                                                    
    bio = forms.CharField(widget=forms.Textarea(attrs={'cols': 80, 'rows': 20}))
    class Meta:                                                               
        model = User                                                          
        fields = ('first_name', 'last_name', 'email', 'bio')                  

Finally, you’ll need to do something in your view manually to save this extra field, as the User model doesn’t know about it.

1👍

It is not recommended to directly alter django tables like auth_user although for the above to work, you will have to change the auth model in django.contrib.auth.models to add the bio field.

Recommended way of doing things is via the UserProfile which has a OneToOne relation with the User Table.

Leave a comment