没有模型错误的用户名属性,即使已经有对象= UserManager()?
No username attribute of model error, even already have objects = UserManager()?
这是我在 user
应用程序中的模型
from django.db import models
from django.contrib.auth.models import AbstractBaseUser, BaseUserManager, PermissionsMixin
class UserTable(AbstractBaseUser, PermissionsMixin):
USERNAME_FIELD='email'
objects = UserManager()
email = models.EmailField(max_length=255, unique=True)
name = models.CharField(max_length=255)
phone = models.CharField(max_length=255, default=None)
is_active = models.BooleanField(default=False)
这是我在 user
应用程序中的 admin.py
。
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from .models import UserTable
admin.site.register(UserTable, UserAdmin)
我也包括在我的 settings.py
INSTALLED_APPS = [
....
'user.apps.UserConfig',
....
]
这是我运行python manage.py migrate
时的错误
(admin.E033) The value of 'ordering[0]' refers to 'username', which is not an attribute of 'user.UserTable'.
The value of 'list_display[0]' refers to 'username', which is not a callable, an attribute of 'UserAdmin', or an attribute or method on 'user.UserTable'.
我不知道为什么,我想当我设置objects = UserManager()
字段时,username
、first_name
、last_name
都设置好了。我还使用 USERNAME_FIELD
设置电子邮件替换 username
主键。
你应该自己写 UserManager
from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
from django.contrib.auth.models import PermissionsMixin
class UserManager(BaseUserManager):
def create_user(self, email, password, **extra_fields):
if not email:
raise ValueError('User must have an email address')
user = self.model(
email=self.normalize_email(email),
**extra_fields
)
user.set_password(password)
user.save()
return user
def create_superuser(self, email, password, **extra_fields):
user = self.create_user(
email,
password,
**extra_fields
)
return user
只是在UserTable
模型中的一个小评论,使
is_active = models.BooleanField(default=True)
可以登录
并在 settings.py
文件中添加
AUTH_USER_MODEL = 'user.UserTable'
编辑
我注意到你调用 AbstractBaseUser, BaseUserManager
的方式不对
from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
from django.contrib.auth.models import PermissionsMixin
试试看,有什么问题告诉我!
问题是因为您继承自 AbstractBaseUser
(没有 username
字段):
class AbstractBaseUser(models.Model):
password = models.CharField(_('password'), max_length=128)
last_login = models.DateTimeField(_('last login'), blank=True, null=True)
... # No definition for usename in this class
然后使用 UserAdmin
(需要 username
作为字段)作为您的管理员:
@admin.register(User)
class UserAdmin(admin.ModelAdmin):
...
# The admin needs the username field as described by ordering
# But your custom user model doesn't have it
ordering = ('username',)
有多种方法可以解决此问题,但一种方法是为您的自定义用户定义您自己的模型管理员:
from django.contrib import admin
class MyAdmin(admin.ModelAdmin):
pass
admin.site.register(UserTable, MyAdmin)
这是我在 user
应用程序中的模型
from django.db import models
from django.contrib.auth.models import AbstractBaseUser, BaseUserManager, PermissionsMixin
class UserTable(AbstractBaseUser, PermissionsMixin):
USERNAME_FIELD='email'
objects = UserManager()
email = models.EmailField(max_length=255, unique=True)
name = models.CharField(max_length=255)
phone = models.CharField(max_length=255, default=None)
is_active = models.BooleanField(default=False)
这是我在 user
应用程序中的 admin.py
。
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from .models import UserTable
admin.site.register(UserTable, UserAdmin)
我也包括在我的 settings.py
INSTALLED_APPS = [
....
'user.apps.UserConfig',
....
]
这是我运行python manage.py migrate
(admin.E033) The value of 'ordering[0]' refers to 'username', which is not an attribute of 'user.UserTable'.
The value of 'list_display[0]' refers to 'username', which is not a callable, an attribute of 'UserAdmin', or an attribute or method on 'user.UserTable'.
我不知道为什么,我想当我设置objects = UserManager()
字段时,username
、first_name
、last_name
都设置好了。我还使用 USERNAME_FIELD
设置电子邮件替换 username
主键。
你应该自己写 UserManager
from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
from django.contrib.auth.models import PermissionsMixin
class UserManager(BaseUserManager):
def create_user(self, email, password, **extra_fields):
if not email:
raise ValueError('User must have an email address')
user = self.model(
email=self.normalize_email(email),
**extra_fields
)
user.set_password(password)
user.save()
return user
def create_superuser(self, email, password, **extra_fields):
user = self.create_user(
email,
password,
**extra_fields
)
return user
只是在UserTable
模型中的一个小评论,使
is_active = models.BooleanField(default=True)
可以登录
并在 settings.py
文件中添加
AUTH_USER_MODEL = 'user.UserTable'
编辑
我注意到你调用 AbstractBaseUser, BaseUserManager
的方式不对
from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
from django.contrib.auth.models import PermissionsMixin
试试看,有什么问题告诉我!
问题是因为您继承自 AbstractBaseUser
(没有 username
字段):
class AbstractBaseUser(models.Model):
password = models.CharField(_('password'), max_length=128)
last_login = models.DateTimeField(_('last login'), blank=True, null=True)
... # No definition for usename in this class
然后使用 UserAdmin
(需要 username
作为字段)作为您的管理员:
@admin.register(User)
class UserAdmin(admin.ModelAdmin):
...
# The admin needs the username field as described by ordering
# But your custom user model doesn't have it
ordering = ('username',)
有多种方法可以解决此问题,但一种方法是为您的自定义用户定义您自己的模型管理员:
from django.contrib import admin
class MyAdmin(admin.ModelAdmin):
pass
admin.site.register(UserTable, MyAdmin)