Question

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'
Was it helpful?

Solution

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.

OTHER TIPS

I got the answer, and this is how you frame your code


    def create_superuser(self, email, name, password):
        """
        Creates and saves a superuser with the given email, name and password.
        """
        user = self.create_user(
            email,
            name,
            password=password,
        )
        user.is_admin = True
        user.save(using=self._db)
        return user


Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top