我有一个需要将用户名字段更改为Autofield
的应用程序。
用例-我正在构建一个应用程序,在该应用程序中,当创建用户时,用户名应类似于银行应用程序中的CustomerID(整数)。
有两种方法可以做到:-
1)拥有id
(PK),它是其默认实现。复制对象的id
并将其分配给用户名列,以允许用户通过ID登录。 权衡
2)允许通过ID登录,更新AuthBackend并也允许通过ID登录。 权衡
Pheww,足够多的讨论。现在是实现(问题)部分。:-
这是我的模特。
class User(AbstractUser):
username = models.BigAutoField(primary_key=True)
user_id = models.UUIDField(default=uuid.uuid4, unique=True,
editable=False)
一个模型不能包含多个AutoField
,因此username
是PK。
我的迁移工作非常顺利。并创建了表。
但是,当我使用创建用户
时User.objects.create_user(email=email, password=password) #expecting the username to be auto-generated.
说用户名是必填项。
我检查了Django code UserManager
function create_user
,并更新了参数username=None
class CustomUserManager(UserManager):
use_in_migrations = True
def create_user(self, username=None, email=None, password=None,
**extra_fields):
return super(CustomUserManager, self).create_user(username=username,
email=email,
password=password,
extra_fields=extra_fields)
它仍然引发异常:-
In [4]: User.objects.create_user(email='ab@ab.com', password='abcd1234@')
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-4-de9c49dd635d> in <module>()
----> 1 User.objects.create_user(email='ab@ab.com', password='abcd1234@')
/Users/praful/Desktop/code/seynse/authentication/app/modules/users/models.py in create_user(self, username, email, password, **extra_fields)
17 email=email,
18 password=password,
---> 19 extra_fields=extra_fields)
20
21
/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/contrib/auth/models.py in create_user(self, username, email, password, **extra_fields)
157 extra_fields.setdefault('is_staff', False)
158 extra_fields.setdefault('is_superuser', False)
--> 159 return self._create_user(username, email, password, **extra_fields)
160
161 def create_superuser(self, username, email, password, **extra_fields):
/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/contrib/auth/models.py in _create_user(self, username, email, password, **extra_fields)
146 """
147 if not username:
--> 148 raise ValueError('The given username must be set')
149 email = self.normalize_email(email)
150 username = self.model.normalize_username(username)
ValueError: The given username must be set
在这种情况下可以做什么?如何将用户名更改为AutoField
。
答案 0 :(得分:0)
您必须为用户模型创建自定义管理器。它可以继承自BaseManager
,但不能继承自默认UserManager
:
class CustomUserManager(BaseUserManager):
use_in_migrations = True
def _create_user(self, email, password, **extra_fields):
"""
Create and save a user with the given email, and password.
"""
email = self.normalize_email(email)
user = self.model(email=email, **extra_fields)
user.set_password(password)
user.save(using=self._db)
return user
def create_user(self, email=None, password=None, **extra_fields):
extra_fields.setdefault('is_staff', False)
extra_fields.setdefault('is_superuser', False)
return self._create_user(email, password, **extra_fields)
def create_superuser(self, email, password, **extra_fields):
extra_fields.setdefault('is_staff', True)
extra_fields.setdefault('is_superuser', True)
if extra_fields.get('is_staff') is not True:
raise ValueError('Superuser must have is_staff=True.')
if extra_fields.get('is_superuser') is not True:
raise ValueError('Superuser must have is_superuser=True.')
return self._create_user(email, password, **extra_fields)
class User(AbstractUser):
username = models.BigAutoField(primary_key=True)
user_id = models.UUIDField(default=uuid.uuid4, unique=True,
editable=False)
objects = CustomUserManager()
def __str__(self):
return str(self.username)
请注意,我已经完全删除了username
,因为没有必要为AutoField传递显式值。