[Answered ]-How to change the field of username to user_name in to django custom user model?

1👍

in admin.py where you are registering your User model. you are registering it with ModelAdmin and in that ModelAdmion you have named field incorrectly. change it to user_name their too.

0👍

In your case, you don’t need to change the field name, just simply override the username field with the db_column='user_name' parameter and argument:

class CustomUser(AbstractBaseUser):
    username_validator = UnicodeUsernameValidator()

    username = models.CharField(
        _('username'),
        max_length=150,
        unique=True,
        help_text=_('Required. 150 characters or fewer. Letters, digits and @/./+/-/_ only.'),
        db_column='user_name'  # <------------------------
        validators=[username_validator],
        error_messages={
            'unique': _("A user with that username already exists."),
        },
    )

Django doc refrence

Leave a comment