1

I can't create superuser in Django after customising userprofile.

When I run manage.py syncdb:

TypeError: create_superuser() takes exactly 4 arguments (3 given)

This is my userprofile model:

    from django.db import models
from django.contrib.auth.models import BaseUserManager, AbstractBaseUser, PermissionsMixin
from django.core.validators import RegexValidator

class AuthUserManager(BaseUserManager):
    def create_user(self, username, email, password=None):
        if not username:
            raise ValueError('User must have username')
        if not email:
            raise ValueError('User must have email')
        user = self.model(username=username, email=email.self.normalize_email(email))
        user.is_active = True
        user.set_password(password)
        user.save(using=self._db)
        return user

    def create_superuser(self, username, email, password):
        user = self.create_user(username=username, email=email, password=password)
        user.is_staff = True
        user.is_superuser = True
        user.save(using=self.db)
        return user

class AuthUser(AbstractBaseUser, PermissionsMixin):
    alphanumeric = RegexValidator(r'^[0-9a-zA-Z]*$', message='Only alphanumeric characters are allowed')

    username = models.CharField(unique=True, max_length=30, validators=[alphanumeric])
    email = models.EmailField(verbose_name='email field', unique=True, max_length=255)
    date_joined = models.DateTimeField(auto_now_add=True)
    is_active = models.BooleanField(default=True, null=False)
    is_staff = models.BooleanField(default=False, null=False)

    objects = AuthUserManager()

    USERNAME_FIELD = 'email'
    REQUIRES_FIELDS = ['username']

    def get_full_name(self):
        return '%s %s %s'.format(self.last_name, self.first_name, self.second_name)
    def get_short_name(self):
        return self.username
    def __str__(self):
        self.email

As far as I understand, there is some troubles in REQUIRED_FIELDS, but it contains username, so I don't realize this error

Steffen D. Sommer
  • 2,896
  • 2
  • 24
  • 47
Dan
  • 151
  • 1
  • 5
  • This question might help you http://stackoverflow.com/questions/25239164/issue-with-createsuperuser-when-implementing-custom-user-model – zom-pro Aug 28 '15 at 06:49
  • Also, I think you have a typo, it isn't REQUIRED_FIELDS? rather than REQUIRES_FIELD. I have had better results inhereting from AbstracUser rather than AbstractBaseUser. Look here: http://stackoverflow.com/questions/21514354/difference-between-abstractuser-and-abstractbaseuser-in-django – zom-pro Aug 28 '15 at 06:52
  • Official django docs: https://docs.djangoproject.com/en/1.8/topics/auth/customizing/#django.contrib.auth.models.CustomUser.REQUIRED_FIELDS – Dan Aug 28 '15 at 07:10
  • UPD: I corrected some typos in my code and make migrations. Now it woks – Dan Aug 28 '15 at 08:04

0 Answers0