gpt4 book ai didi

python - 类型错误 : create_user() got multiple values for keyword argument 'name'

转载 作者:行者123 更新时间:2023-11-28 22:50:23 27 4
gpt4 key购买 nike

我正在尝试在 Django 中创建自定义身份验证,其中标识符是电子邮件,有一个名为名称的必填字段和一个密码字段。创建 super 用户时,出现错误。

TypeError: create_user() got multiple values for keyword argument 'name'

这是我的模型.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'

这是回溯

 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'

最佳答案

改变你的函数调用

user = self.create_user(email,
password=password,
name=name
)

user = self.create_user(email=email,  # use email=email
password=password,
name=name
)

您的参数顺序不正确。 emailname 之前传递,然后再次将 name 作为关键字参数传递。

关于python - 类型错误 : create_user() got multiple values for keyword argument 'name' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22705943/

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