我已经自定义 BaseUserManager 来创建用户,但是当我从 django 管理面板添加用户时出现“请更正以下错误”的错误,但我找不到问题所在。
模型.py:
class UserManager(BaseUserManager):
def create_user(self,email,password):
user = self.model(email=email)
user.set_password(password)
def create_superuser(self,email,password):
user = self.model(email=email)
user.set_password(password)
user.is_admin = True
user.is_superuser = True
user.is_staff=True
user.save(using=self._db)
class User(AbstractBaseUser,PermissionsMixin):
COMPANY='CO'
EMPLOYEE='EM'
STATUS_CHOICES=(
(COMPANY,'Company'),
(EMPLOYEE,'Employee'),
)
Status=models.CharField(max_length=2,
choices=STATUS_CHOICES,
default=EMPLOYEE)
user_image = models.CharField(max_length=100)
is_admin=models.BooleanField()
email=models.EmailField(unique=True)
is_staff=models.BooleanField(default=False)
object = UserManager()
USERNAME_FIELD='email'
REQUIRED_FIELDS = []
objects=models.Manager()
def get_short_name(self):
return self.email
Admin.py 文件:
class UserAdmin(UserAdmin):
list_display = ('email', 'is_admin')
list_filter = ('is_admin',)
fieldsets = (
(None, {'fields': ('email', 'password','Status','user_image','last_login')}),
('Permissions', {'fields': ('is_admin','is_staff','is_superuser','user_permissions','groups')}),
)
add_fieldsets= (
(None, {'fields': ('email', 'password','Status','user_image','last_login')}),
('Permissions', {'fields': ('is_admin','is_staff','is_superuser','user_permissions','groups')}),
)
search_fields = ('password',)
ordering = ('password',)
filter_horizontal = ()
admin.site.register(User,UserAdmin)
admin.site.register(Company)
admin.site.register(Employee)
admin.site.register(Job)
admin.site.register(AppliedJobs)
有人可以建议我做错了什么吗?当我从管理面板添加用户时,我总是会收到错误消息。我无法弄清楚,因为我是第一次在 baseusermanager 上工作。