'Why is cleaned data returning an exceeded value length from the registration form?

I have a custom user model that has a username field with a max_length=50. Under the custom registration form, it throws me an error when the value of the username` field is just less than 10 characters:

Ensure this value has at most 50 characters (it has 170).

Below are the codes that I used that is in correlation with the username field:

#models.py
class UserAccount(AbstractBaseUser, PermissionsMixin):
    username = models.CharField(null=False, blank=False, max_length=50, unique=True)
#forms.py
class RegisterForm(UserCreationForm):
    username = forms.CharField(widget=TextInput(
        attrs={
            "class": "form-control",
            "id": "username",
            #"placeholder": "Username",
        }))

    class Meta:
        model = UserAccount
        fields = ('username',)

    def clean_username(self):
        username = self.cleaned_data.get("username")
        username_filter = UserAccount.objects.filter(username__iexact=username)
        if username_filter.exists():
            self.add_error('username', "Username is already taken")
        return self.cleaned_data
HTML

<div class="row form-group">
    <div class="col-sm-4 label-column">
        <label class="col-form-label" for="username-input-field">Username </label>
    </div>
    <div class="col-sm-6 input-column">{{register_form.username}}</div>
</div>

The error only occurs when I use the registration form on the html when creating a user, but when I create the user via python manage.py shell and admin panel, it gets created without any error.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source