本文介绍了TypeError:create_user()为关键字参数“名称”获得了多个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试在Django中创建自定义身份验证,其中标识符是电子邮件,这里有一个必填字段,称为name和password字段。创建超级用户时,出现错误。
TypeError:create_user()为关键字参数'name'获得了多个值
这是我的模型。py
<$ p从django.db中导入$ p>
从django.contrib.auth.models中导入
从django.conf中导入用户
从django.contrib.auth中导入设置
。模型导入(
BaseUserManager,AbstractBaseUser
)
class MyUserManager(BaseUserManager):
def create_user(self,name,email,password = None) :
创建并保存具有给定电子邮件,名称和密码的用户。
如果没有电子邮件:
提高ValueError('Users必须具有电子邮件地址')
user = self.model(
name = name,
email = self.normalize_email(email),
)
user.set_password(密码)
user.save(using = self._db)
返回用户
def create_superuser(自己,电子邮件,名称,密码):
创建并保存具有给定电子邮件,名称和密码的超级用户。
用户= self.create_user(电子邮件,
密码=密码,
名称=名称
)
user.is_admin =真实
user.save(using = self._db)
返回用户
class MyUser(AbstractBaseUser):
email = models.EmailField(
verbose_name ='email address',
max_length = 255,
unique = True,
)
name = models.CharField(max_length = 20)
is_active =模型。 BooleanField(default = True)
is_admin = models.BooleanField(default = False)
对象= MyUserManager()
USERNAME_FIELD ='email'
REQUIRED_FIELDS = ['name']
def get_full_name(self):
#用户通过其电子邮件地址
标识return self.email
def get_short_name(self):
#用户通过其电子邮件地址标识
return self.email
#在Python 3上: def __str __(self):
def __unicode __(self):
返回self.name
def has_perm(self,perm,obj = None):
做用户是否具有特定权限?
#最简单的答案:是,始终
返回True
def has_module_perms(self,app_label):
用户有权查看应用ʻapp_label`?
#最简单的答案:是,始终
返回True
@property
def is_staff(self):
用户是工作人员吗?
#最简单的答案:所有管理员都是工作人员
return self.is_admin
class MyUser(AbstractBaseUser ):
名称= models.CharField(max_length = 20)
电子邮件= models.CharField(max_length = 40,unique = True)
REQUIRED_FIELDS = ['name']
USERNAME_FIELD ='电子邮件'
这是回溯
文件 / usr / local /lib/python2.7/dist-packages/django/contrib/auth/management/commands/createsuperuser.py,第141行,位于句柄
self.UserModel._default_manager.db_manager(database).create_superuser(** user_data)
文件 /home/jaskaran/coding/django/TaskMan/taskmanager/models.py,行32,在create_superuser中
name = name
TypeError:create_user()获得多个值关键字参数名称
解决方案
更改函数调用
user = self.create_user(电子邮件,
密码=密码,
名称=名称
)
至
user = self.create_user(email = email,#use email = email
password = password,
name = name
)
您的参数顺序不正确。在名称
之前传递电子邮件
,然后再次输入名称
作为关键字参数传递。
I'm trying to create custom authentication in Django where the identifier is an email, there is a required field called name and a password field. While creating the superuser, i'm getting an error.
TypeError: create_user() got multiple values for keyword argument 'name'
Here is my models.py
from django.db import models
from django.contrib.auth.models import User
from django.conf import settings
from django.contrib.auth.models import (
BaseUserManager, AbstractBaseUser
)
class MyUserManager(BaseUserManager):
def create_user(self, name, email, password=None):
"""
Creates and saves a User with the given email, name and password.
"""
if not email:
raise ValueError('Users must have an email address')
user = self.model(
name=name,
email=self.normalize_email(email),
)
user.set_password(password)
user.save(using=self._db)
return user
def create_superuser(self, email, name, password):
"""
Creates and saves a superuser with the given email, name and password.
"""
user = self.create_user(email,
password=password,
name=name
)
user.is_admin = True
user.save(using=self._db)
return user
class MyUser(AbstractBaseUser):
email = models.EmailField(
verbose_name='email address',
max_length=255,
unique=True,
)
name = models.CharField(max_length=20)
is_active = models.BooleanField(default=True)
is_admin = models.BooleanField(default=False)
objects = MyUserManager()
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = ['name']
def get_full_name(self):
# The user is identified by their email address
return self.email
def get_short_name(self):
# The user is identified by their email address
return self.email
# On Python 3: def __str__(self):
def __unicode__(self):
return self.name
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
class MyUser(AbstractBaseUser):
name = models.CharField(max_length=20)
email = models.CharField(max_length=40, unique=True)
REQUIRED_FIELDS = ['name']
USERNAME_FIELD = 'email'
Here is the traceback
File "/usr/local/lib/python2.7/dist-packages/django/contrib/auth/management/commands/createsuperuser.py", line 141, in handle
self.UserModel._default_manager.db_manager(database).create_superuser(**user_data)
File "/home/jaskaran/coding/django/TaskMan/taskmanager/models.py", line 32, in create_superuser
name=name
TypeError: create_user() got multiple values for keyword argument 'name'
解决方案
Change your function call
user = self.create_user(email,
password=password,
name=name
)
to
user = self.create_user(email=email, # use email=email
password=password,
name=name
)
The order of your parameters is not correct. The email
is passed before name
and then again name
is passed as keyword parameter.
这篇关于TypeError:create_user()为关键字参数“名称”获得了多个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!