[Answered ]-Custom fields in user model Django

1👍

Django’s User model [Django-doc] already has a first_name and last_name, you can subclass the UserCreationForm and add fields for the first_name and last_name:

from django.contrib.auth.forms import UserCreationForm

class UserRegisterForm(UserCreationForm):

    def __init__(self, *args, **kwargs):
        super(UserRegisterForm, self).__init__(*args, **kwargs)
        self.fields['password1'].label = 'Hasło'
        self.fields['password2'].label = 'Powtórz hasło'

    email = forms.EmailField()
    first_name = forms.CharField(
        label='Imię',
        required=True,
        max_length=30,
    )
    last_name = forms.CharField(
        label='Nazwisko',
        required=True,
        max_length=30,
    )

    class Meta:
        model = User
        fields = ['username', 'email', 'password1', 'password2', 'first_name', 'last_name']
        labels = {
            'username': 'Nazwa użytkownika',
        }

Leave a comment