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.
CodePudding user response:
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.