TypeError:create_user()获取关键字参数'name'的多个值

时间:2014-03-28 06:44:07

标签: python django django-authentication

我试图在Django中创建自定义身份验证,其中标识符是电子邮件,有一个名为name和password字段的必填字段。在创建超级用户时,我收到了错误。

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

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

这是追溯

 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'

1 个答案:

答案 0 :(得分:4)

更改您的函数调用

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

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

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