what is fields and field_classes in django UserCreationForm?

Viewed 89

im new to learning django and i wanted to make signup form in django but i see this part of code and i didnt understand it what are these code doing and why we use them

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."),
    )

    class Meta:
        model = User
        fields = ("username",)
        field_classes = {'username': UsernameField}

can somebody explain what are

```fields = ("username",)
        field_classes = {'username': UsernameField}```

and why we use them?

1 Answers
fields = ("username",)

This means that you only want the username field from your User model.

For the rest of the fields (password1, password2, error_messages), you have a custom implementation.


field_classes = {'username': UsernameField}

This means that the class you want to use for username field is UsernameField Class, if that makes sense.

Related