gpt4 book ai didi

python - Django 自定义用户 - 不使用用户名 - 用户名唯一约束失败

转载 作者:行者123 更新时间:2023-12-03 23:05:41 25 4
gpt4 key购买 nike

我按照文档的建议,通过对 AbstractBaseUser 进行子类化来创建自己的用户模型。这里的目标是使用一个名为 mob_phone 的新字段作为注册和登录的识别字段。
它很有魅力 - 对于第一个用户。它将用户名字段设置为空 - 空白。但是当我注册第二个用户时,我收到“唯一约束失败:user_account_customuser.username”。
我基本上想完全取消用户名字段。我怎样才能做到这一点?
我基本上需要找到一种方法使用户名字段不唯一或完全删除它。
模型.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 (most recent call last):File "manage.py", line 22, in execute_from_command_line(sys.argv)File "/home/dean/.local/lib/python3.5/site-packages/django/core/management/init.py", line 363, in execute_from_command_lineutility.execute()File "/home/dean/.local/lib/python3.5/site-packages/django/core/management/init.py", line 355, in executeself.fetch_command(subcommand).run_from_argv(self.argv)File "/home/dean/.local/lib/python3.5/site-packages/django/core/management/base.py", line 283, in run_from_argvself.execute(*args, **cmd_options)File "/home/dean/.local/lib/python3.5/site-packages/django/contrib/auth/management/commands/createsuperuser.py", line 63, in executereturn super(Command, self).execute(*args, **options)File "/home/dean/.local/lib/python3.5/site-packages/django/core/management/base.py", line 330, in executeoutput = self.handle(*args, **options)File "/home/dean/.local/lib/python3.5/site-packages/django/contrib/auth/management/commands/createsuperuser.py", line 183, in handleself.UserModel._default_manager.db_manager(database).create_superuser(**user_data)File "/home/dean/Development/UrbanFox/UrbanFox/user_account/models.py", line 43, in create_superuserpassword=passwordFile "/home/dean/Development/UrbanFox/UrbanFox/user_account/models.py", line 32, in create_useruser.save(using=self._db)File "/home/dean/.local/lib/python3.5/site-packages/django/contrib/auth/base_user.py", line 80, in savesuper(AbstractBaseUser, self).save(*args, **kwargs)File "/home/dean/.local/lib/python3.5/site-packages/django/db/models/base.py", line 807, in saveforce_update=force_update, update_fields=update_fields)File "/home/dean/.local/lib/python3.5/site-packages/django/db/models/base.py", line 837, in save_baseupdated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)File "/home/dean/.local/lib/python3.5/site-packages/django/db/models/base.py", line 923, in _save_tableresult = self._do_insert(cls._base_manager, using, fields, update_pk, raw)File "/home/dean/.local/lib/python3.5/site-packages/django/db/models/base.py", line 962, in _do_insertusing=using, raw=raw)File "/home/dean/.local/lib/python3.5/site-packages/django/db/models/manager.py", line 85, in manager_methodreturn getattr(self.get_queryset(), name)(*args, **kwargs)File "/home/dean/.local/lib/python3.5/site-packages/django/db/models/query.py", line 1076, in _insertreturn query.get_compiler(using=using).execute_sql(return_id)File "/home/dean/.local/lib/python3.5/site-packages/django/db/models/sql/compiler.py", line 1107, in execute_sqlcursor.execute(sql, params)File "/home/dean/.local/lib/python3.5/site-packages/django/db/backends/utils.py", line 80, in executereturn super(CursorDebugWrapper, self).execute(sql, params)File "/home/dean/.local/lib/python3.5/site-packages/django/db/backends/utils.py", line 65, in executereturn self.cursor.execute(sql, params)File "/home/dean/.local/lib/python3.5/site-packages/django/db/utils.py", line 94, in exitsix.reraise(dj_exc_type, dj_exc_value, traceback)File "/home/dean/.local/lib/python3.5/site-packages/django/utils/six.py", line 685, in reraiseraise value.with_traceback(tb)File "/home/dean/.local/lib/python3.5/site-packages/django/db/backends/utils.py", line 65, in executereturn self.cursor.execute(sql, params)File "/home/dean/.local/lib/python3.5/site-packages/django/db/backends/sqlite3/base.py", line 328, in executereturn Database.Cursor.execute(self, query, params)django.db.utils.IntegrityError: UNIQUE constraint failed: user_account_customuser.username

最佳答案

好吧,我是个白痴。发布后几秒钟,我想到了明显的解决方案:

    username = models.CharField(max_length=40, unique=False, default='')

只需覆盖用户名字段并使其不唯一。

橡皮鸭理论在行动......

关于python - Django 自定义用户 - 不使用用户名 - 用户名唯一约束失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45619217/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com