【发布时间】:2018-01-19 00:40:15
【问题描述】:
按照文档的建议,我通过对 AbstractBaseUser 进行子类化来创建自己的用户模型。这里的目标是使用一个名为 mob_phone 的新字段作为注册和登录的标识字段。
它很有魅力 - 对于第一个用户。它将用户名字段设置为空 - 空白。但是当我注册第二个用户时,我得到“唯一约束失败:user_account_customuser.username”。
我基本上想完全取消用户名字段。我怎样才能做到这一点?
我基本上需要找到一种方法使用户名字段不唯一或完全删除它。
models.py
from django.contrib.auth.models import AbstractUser, BaseUserManager
class MyUserManager(BaseUserManager):
def create_user(self, mob_phone, email, password=None):
"""
Creates and saves a User with the given mobile number and password.
"""
if not mob_phone:
raise ValueError('Users must mobile phone number')
user = self.model(
mob_phone=mob_phone,
email=email
)
user.set_password(password)
user.save(using=self._db)
return user
def create_superuser(self, mob_phone, email, password):
"""
Creates and saves a superuser with the given email, date of
birth and password.
"""
user = self.create_user(
mob_phone=mob_phone,
email=email,
password=password
)
user.is_admin = True
user.save(using=self._db)
return user
class CustomUser(AbstractUser):
mob_phone = models.CharField(blank=False, max_length=10, unique=True)
is_admin = models.BooleanField(default=False)
objects = MyUserManager()
# override username field as indentifier field
USERNAME_FIELD = 'mob_phone'
EMAIL_FIELD = 'email'
def get_full_name(self):
return self.mob_phone
def get_short_name(self):
return self.mob_phone
def __str__(self): # __unicode__ on Python 2
return self.mob_phone
def has_perm(self, perm, obj=None):
"Does the user have a specific permission?"
# Simplest possible answer: Yes, always
return True
def has_module_perms(self, app_label):
"Does the user have permissions to view the app `app_label`?"
# Simplest possible answer: Yes, always
return True
@property
def is_staff(self):
"Is the user a member of staff?"
# Simplest possible answer: All admins are staff
return self.is_admin
堆栈跟踪:
Traceback(最近一次调用最后一次): 文件“manage.py”,第 22 行,在 execute_from_command_line(sys.argv) 文件“/home/dean/.local/lib/python3.5/site-packages/django/core/management/init.py”,第 363 行,在 execute_from_command_line 实用程序.execute() 执行中的文件“/home/dean/.local/lib/python3.5/site-packages/django/core/management/init.py”,第 355 行 self.fetch_command(子命令).run_from_argv(self.argv) 文件“/home/dean/.local/lib/python3.5/site-packages/django/core/management/base.py”,第 283 行,在 run_from_argv self.execute(*args, **cmd_options) 文件“/home/dean/.local/lib/python3.5/site-packages/django/contrib/auth/management/commands/createsuperuser.py”,第63行,在执行 return super(Command, self).execute(*args, **options) 文件“/home/dean/.local/lib/python3.5/site-packages/django/core/management/base.py”,第330行,在执行 输出 = self.handle(*args, **options) 文件“/home/dean/.local/lib/python3.5/site-packages/django/contrib/auth/management/commands/createsuperuser.py”,第 183 行,在句柄中 self.UserModel._default_manager.db_manager(数据库).create_superuser(**user_data) 文件“/home/dean/Development/UrbanFox/UrbanFox/user_account/models.py”,第 43 行,在 create_superuser 密码=密码 文件“/home/dean/Development/UrbanFox/UrbanFox/user_account/models.py”,第 32 行,在 create_user user.save(使用=self._db) 文件“/home/dean/.local/lib/python3.5/site-packages/django/contrib/auth/base_user.py”,第 80 行,保存 super(AbstractBaseUser, self).save(*args, **kwargs) 文件“/home/dean/.local/lib/python3.5/site-packages/django/db/models/base.py”,第 807 行,保存 force_update=force_update,update_fields=update_fields) 文件“/home/dean/.local/lib/python3.5/site-packages/django/db/models/base.py”,第 837 行,在 save_base 更新 = self._save_table(raw, cls, force_insert, force_update, using, update_fields) _save_table 中的文件“/home/dean/.local/lib/python3.5/site-packages/django/db/models/base.py”,第 923 行 结果 = self._do_insert(cls._base_manager, using, fields, update_pk, raw) _do_insert 中的文件“/home/dean/.local/lib/python3.5/site-packages/django/db/models/base.py”,第 962 行 使用=使用,原始=原始) 文件“/home/dean/.local/lib/python3.5/site-packages/django/db/models/manager.py”,第 85 行,在 manager_method 返回 getattr(self.get_queryset(), name)(*args, **kwargs) 文件“/home/dean/.local/lib/python3.5/site-packages/django/db/models/query.py”,第 1076 行,在 _insert return query.get_compiler(using=using).execute_sql(return_id) 文件“/home/dean/.local/lib/python3.5/site-packages/django/db/models/sql/compiler.py”,第 1107 行,在 execute_sql cursor.execute(sql,参数) 文件“/home/dean/.local/lib/python3.5/site-packages/django/db/backends/utils.py”,第 80 行,在执行 return super(CursorDebugWrapper, self).execute(sql, params) 文件“/home/dean/.local/lib/python3.5/site-packages/django/db/backends/utils.py”,第 65 行,在执行 返回 self.cursor.execute(sql, params) exit 中的文件“/home/dean/.local/lib/python3.5/site-packages/django/db/utils.py”,第 94 行 六.reraise(dj_exc_type,dj_exc_value,回溯) 文件“/home/dean/.local/lib/python3.5/site-packages/django/utils/six.py”,第 685 行,在 reraise 提高 value.with_traceback(tb) 文件“/home/dean/.local/lib/python3.5/site-packages/django/db/backends/utils.py”,第 65 行,在执行 返回 self.cursor.execute(sql, params) 执行中的文件“/home/dean/.local/lib/python3.5/site-packages/django/db/backends/sqlite3/base.py”,第 328 行 返回 Database.Cursor.execute(self, query, params) django.db.utils.IntegrityError:唯一约束失败:user_account_customuser.username
【问题讨论】: