在Django中创建职员用户 [英] Create staff user in Django
问题描述
我正在尝试在Django中使用
UserModel.objects.create_user(username = A ,email = a@a.com,密码= a,is_staff = True)
但在Django的UserManager中,位于 https://github.com /django/django/blob/master/django/contrib/auth/models.py 它说
class UserManager (BaseUserManager):
use_in_migrations =真
def _create_user(自己,用户名,电子邮件,密码,
is_staff,is_superuser,** extra_fields):
创建并保存具有给定用户名,电子邮件和密码的用户。
如果不是用户名:
提高ValueError('必须设置给定用户名')
电子邮件= self.normalize_email(电子邮件)
用户= self.model(用户名=用户名,电子邮件=电子邮件,
is_staff = is_staff,is_superuser = is_superuser,
** extra_fields)
user.set_password(password)
user.save(using = self._db)
返回用户
def create_user(self,username,email = None,password = None,** extra_fields):
返回self._create_user(用户名,电子邮件,密码,False,False,
** extra_fields )
def create_superuser(自己,用户名,电子邮件,密码,** extra_fields):
返回self._create_user(用户名,电子邮件,密码,True,True,
** extra_fields)
所以在尝试设置 is_staff = False $ c $时c>除非我使用
create_superuser
而不是,否则它将始终使用
;但是, is_staff = True
覆盖此字段。 create_user create_superuser
也会设置 is_superuser = True
。
他们是否忘了在Django中创建方法 create_staff_user
? :-)
不,我不认为‘他们’忘记了create_staff_method。
这是拉动请求催生了 _create_user
方法,这样做的原因是:
创建超级用户时不要两次发出db信号
create_user和create_superuser方法都使用相同的私有方法。了解这一点,并知道Python中没有真正的私有方法,可以直接使用_create_user。但是这是一个丑陋的选择。
create_user方法是为您提供方便的额外方法。没错,使用 is_staff = True
调用它不会产生预期的结果。我认为这是真正的错误。错误是:
TypeError:_create_user()获得了关键字参数
'is_staff'的多个值
您的代码(用户名是位置参数):
User.objects.create_user('john',email='lennon@thebeatles.com',password ='johnpassword',is_staff = True)
有关创建用户的文档的建议:
user = User.objects.create_user( 'john','lennon@thebeatles.com','johnpassword')
user.is_staff = True
user.save()
但是这种方式我们两次调用保存。 :/
UserManager是BaseUserManager的子类,BaseUserManager是Manager的子类。因此,您也可以这样做:
电子邮件= normalize_email('lennon@thebeatles.com')
用户=用户。 objects.create(username ='john',
email = email,is_staff = True)
user.set_password(password)
user.save()
由于显式优于隐式,简单优于复杂...我会选择最基本,最简单的 User.objects.create
。或等待 https://code.djangoproject.com/ticket/25009 登陆。 p>
I'm trying to create a staff user in Django with
UserModel.objects.create_user(username="A", email="a@a.com", password="a", is_staff=True)
but in UserManager in Django at https://github.com/django/django/blob/master/django/contrib/auth/models.py it says
class UserManager(BaseUserManager):
use_in_migrations = True
def _create_user(self, username, email, password,
is_staff, is_superuser, **extra_fields):
"""
Creates and saves a User with the given username, email and password.
"""
if not username:
raise ValueError('The given username must be set')
email = self.normalize_email(email)
user = self.model(username=username, email=email,
is_staff=is_staff, is_superuser=is_superuser,
**extra_fields)
user.set_password(password)
user.save(using=self._db)
return user
def create_user(self, username, email=None, password=None, **extra_fields):
return self._create_user(username, email, password, False, False,
**extra_fields)
def create_superuser(self, username, email, password, **extra_fields):
return self._create_user(username, email, password, True, True,
**extra_fields)
so when trying to set is_staff = False
it will always override this field with is_staff = True
unless I use create_superuser
instead of create_user
; however, create_superuser
also sets is_superuser = True
.
Have they forgot to create the method create_staff_user
in Django? :-)
No, I don't think that 'they' forgot the create_staff_method.
This is the pull request that gave birth to the _create_user
method and the reason for doing so is:
don't raise db signals twice when creating superuser
Both create_user and create_superuser methods make use of the same private method. Understanding this, and knowing that there are no true private methods in Python makes utilising _create_user directly an option. But an ugly option.
The create_user method is extra method for your convenience. You are right that calling it with is_staff=True
won't give the expected result. I think that is the real bug. The error is:
TypeError: _create_user() got multiple values for keyword argument 'is_staff'
Your code (username is a positional argument):
User.objects.create_user('john', email='lennon@thebeatles.com', password='johnpassword', is_staff=True)
What the documentation on creating users suggest:
user = User.objects.create_user('john', 'lennon@thebeatles.com', 'johnpassword')
user.is_staff=True
user.save()
But this way we are calling save twice. :/
The UserManager is a subclass of BaseUserManager which is a subclass of Manager. So you can also do:
email = normalize_email('lennon@thebeatles.com')
user = User.objects.create(username='john',
email=email, is_staff=True )
user.set_password(password)
user.save()
Since explicit is better than implicit and simple is better than complex... I would go for the most basic and simple User.objects.create
. Or wait for https://code.djangoproject.com/ticket/25009 to land.
这篇关于在Django中创建职员用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!