1

I have created custom user model. now I want to use user_name as username field instead of username. like shown in below code snippet.

class CustomUser(AbstractBaseUser):
    
    username_validator = UnicodeUsernameValidator()

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

i'm unable to do that. i'm getting below error:

SystemCheckError: System check identified some issues:
ERRORS:
<class 'accounts.admin.CustomUserAdmin'>: (admin.E033) The value of 'ordering[0]' refers to 'username', which is not an attribute of 'accounts.CustomUser'.
<class 'accounts.admin.CustomUserAdmin'>: (admin.E108) The value of 'list_display[0]' refers to 'username', which is not a callable, an attribute of 'CustomUserAdmin', or an attribute or method on 'accounts.CustomUser'

the reason of using this, is because all the project's database tables convention is like this. It would be more better if i could define field name is database just as we do for tables in Meta class like below. where i'm calling my customuser model as user model in db.

class Meta:
        db_table = "user"

is there anyway to call table field like this way ?

class Meta:
            db_table_user_name = "username"

if it possible then we dont need to change username to user_name. we can directly call username field is equal to user_name in database. if and only if it is possible with django models.

Benyamin Jafari
  • 27,880
  • 26
  • 135
  • 150
Ahmed Yasin
  • 886
  • 1
  • 7
  • 31

2 Answers2

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.

Abdul Raffay
  • 169
  • 7
  • brother can i change the label of field in database so that models field still stays username but i label it to user_name for database? – Ahmed Yasin Dec 31 '21 at 10:44
  • you can user verbose_name argument if that's what you are looking for name = models.CharField(max_length=200, verbose_name="Nom") – Abdul Raffay Dec 31 '21 at 10:49
  • brother one more thing.. as you can see it my abstractbaseuser class. Now i'm getting last login field but i dont want that.. How can i remove that ? – Ahmed Yasin Dec 31 '21 at 11:10
  • remove last login field from CustomUserAdmin in admin.py – Abdul Raffay Dec 31 '21 at 11:12
  • i didn't use that in admin `class CustomUserAdmin(admin.ModelAdmin): add_fieldsets = ( (None, { 'classes': ('wide',), 'fields': ('username', 'password1', 'password2'), }), ) list_display = ('username', 'email', 'is_staff') search_fields = ('username', 'email') ordering = ('username',)` – Ahmed Yasin Dec 31 '21 at 11:13
  • you want it completely removed or you just dont want to show it in admin panel? – Abdul Raffay Dec 31 '21 at 11:16
  • I want to completely remove it – Ahmed Yasin Dec 31 '21 at 11:17
  • django's AbstractBaseUser has last_login field by default. you can create your own AbstractBaseUser class and inherit your model with that instead of django's AbstractBaseUser. check this answer https://stackoverflow.com/a/70518925/12009358 – Abdul Raffay Dec 31 '21 at 11:21
  • can i override it and change into null and blank true ? – Ahmed Yasin Dec 31 '21 at 11:22
  • yeah you can do that too – Abdul Raffay Dec 31 '21 at 11:24
  • you're welcome brother. remember changing into null will not remove that field completely. it'll be there but will have null values – Abdul Raffay Dec 31 '21 at 11:34
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

Benyamin Jafari
  • 27,880
  • 26
  • 135
  • 150