Search code examples
pythondjangosuperuser

django can't create superuser


I want code a blog with Django. Default User model is not suitable for my blog, I want to code my own User.

from django.contrib.auth.models import AbstractBaseUser, BaseUserManager
from django.db import models

class MyUserManager(BaseUserManager):
    def create_superuser(self, email, username, password, created_at):
        user = self.create_user(email=email,
            username=username,
            password = password,
            created_at = created_at
            )
        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,
        )

    username = models.CharField(
        max_length = 100,
        unique = True,
        db_index = True,
        )

    created_at = models.DateField()

    is_active = models.BooleanField(default = True)
    is_admin = models.BooleanField(default = False)

    objects = MyUserManager()

    USERNAME_FIELD = 'email'
    REQUIRED_FIELDS = ['username']

    def get_full_name(self):
        return self.email

    def get_short_name(self):
        return self.username

    def __unicode__(self):
        return self.email

    def has_perm(self, perm, obj=None):
        return True

    def has_module_perms(self, app_label):
        return True

    @property
    def is_staff(self):
        return self.is_admin

It has a mistake.enter image description here

I follow the tutorial instructions. I don't know why this problem occurs.

I post MyUser model, hope can help answer this question.

What can I do?


Solution

  • You should be constructing an instance of MyUserManager and calling its create_superuser method with all of the positional arguments. You are getting the TypeError because the framework is calling create_superuser without the appropriate positional arguments. If you are receiving this error when you run a manage.py task, such as manage.py createsuperuser then it's because manage.py is attempting to call create_superuser without getting the right number of arguments together. These arguments are handled by setting the appropriate fields in your MyUser object:

    Accounting for the additional arguments in your create_superuser method

    USERNAME_FIELD = 'username'
    REQUIRED_FIELDS = ['email', 'created_at']
    

    There are other serious problems with your code. From the "Customizing Authentication" documentation you'll see that you must extend create_user and create_superuser in both cases, the username_field is the first positional argument and in create_superuser, password is the second positional field. I have modified your code to include the create_user method within the MyUserManager class.

    from django.contrib.auth.models import AbstractBaseUser, BaseUserManager
    from django.db import models
    
    class MyUserManager(BaseUserManager):
        def create_user(self, username, password, email, created_at):
            """
            Creates and saves a User with the given username, password, email, created_a
            birth and password.
            """
            if not username:
                raise ValueError('Users must have a username')
            if not email:
                raise ValueError('Users must have an email address')
    
            user = self.model(
                username=username,
                email=self.normalize_email(email),
                created_at=created_at,
            )
            user.set_password(password)
            user.save(using=self._db)
            return user
    
        def create_superuser(self, username, password, email, created_at):
            user = self.create_user(username, password, email, created_at)
            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,
            )
    
        username = models.CharField(
            max_length = 100,
            unique = True,
            db_index = True,
            )
    
        created_at = models.DateField()
    
        is_active = models.BooleanField(default = True)
        is_admin = models.BooleanField(default = False)
    
        objects = MyUserManager()
    
        USERNAME_FIELD = 'username'
        REQUIRED_FIELDS = ['email', 'created_at']
    
        def get_full_name(self):
            return self.email
    
        def get_short_name(self):
            return self.username
    
        def __unicode__(self):
            return self.email
    
        def has_perm(self, perm, obj=None):
            return True
    
        def has_module_perms(self, app_label):
            return True
    
        @property
        def is_staff(self):
            return self.is_admin
    

    Finally, you'll have to finish running through the example to get it all wired up appropriately, follow along with the full example