John
John

Reputation: 161

AttributeError when building a custom users app in django

I am making a web tool with few apps in django. one of the apps is called "users" for which I made a custom app. I tried to create a superuser using the following command:

python manage.py createsuperuser

I was expecting the program to ask me for username, password, email and phone number but I was asked only for:

username, password

but I got the following error:

Traceback (most recent call last):
  File "manage.py", line 22, in <module>
    main()
  File "manage.py", line 18, in main
    execute_from_command_line(sys.argv)
  File "/home/john/PycharmProjects/digital_products/venv/lib/python3.6/site-packages/django/core/management/__init__.py", line 419, in execute_from_command_line
    utility.execute()
  File "/home/john/PycharmProjects/digital_products/venv/lib/python3.6/site-packages/django/core/management/__init__.py", line 413, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/john/PycharmProjects/digital_products/venv/lib/python3.6/site-packages/django/core/management/base.py", line 354, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/home/john/PycharmProjects/digital_products/venv/lib/python3.6/site-packages/django/contrib/auth/management/commands/createsuperuser.py", line 79, in execute
    return super().execute(*args, **options)
  File "/home/john/PycharmProjects/digital_products/venv/lib/python3.6/site-packages/django/core/management/base.py", line 398, in execute
    output = self.handle(*args, **options)
  File "/home/john/PycharmProjects/digital_products/venv/lib/python3.6/site-packages/django/contrib/auth/management/commands/createsuperuser.py", line 189, in handle
    self.UserModel._default_manager.db_manager(database).create_superuser(**user_data)
AttributeError: 'UserManager' object has no attribute 'create_superuser'

here is the models.py for the users app:

import random

from django.db import models
from django.utils.translation import ugettext_lazy as _
from django.core import validators
from django.utils import timezone
from django.contrib.auth.models import AbstractBaseUser, PermissionsMixin, BaseUserManager, send_mail


class UserManager(BaseUserManager):
    use_in_migrations = True

    def _create_user(self, username, phone_number, email, password, is_staff, is_superuser, **extra_fields):

        now = timezone.now()
        if not username:
            raise ValueError('The given username must be set')
        email = self.normalize_email(email)
        user = self.model(phone_number=phone_number,
                          username=Username, email=email,
                          is_staff=is_staff, is_active=True,
                          is_superuser=is_superuser,
                          date_joined=now, **extra_fields)

        if not extra_fields.get('no_password'):
            user.set_password(password)

        user.save(using=self._db)
        return user

    def create_user(self, username=None, phone_number=None, email=None, password=None, **extra_fields):
        if username is None:
            if email:
                username = email.split('@', 1)[0]
            if phone_number:
                username = random.choice('abcdefghijklmnopqrstuvwxyz') + str(phone_number)[-7:]
            while User.objects.filter(username=username).exists():
                username += str(random.randint(10, 99))

        return self._create_user(username, phone_number, email, password, False, False, **extra_fields)


    def create_superuser(self, username, phone_number, email, password, **extra_fields):
        return self._create_user(username, phone_number, email, password, True, True, **extra_fields)


    def get_by_phone_number(self, phone_number):
        return self.get(**{'phone_number': phone_number})



class User(AbstractBaseUser, PermissionsMixin):

    username = models.CharField(_('username'), max_length=32, unique=True,
                                help_text=
                                    'Required. 30 characters or fewer starting with a letter. Letters, digits',
                                validators=[
                                    validators.RegexValidator(r'^[a-zA-Z][a-zA-Z0-9_\.]+$',
                                                              _('Enter a valid username starting with a-z.'
                                                                'This value may contain only letters and numbers'
                                                                'and underscore characters.'), 'invalid')
                                ],
                                error_messages={
                                    'unique':_('A user with that username already exists.'),
                                }
                                )
    first_name = models.CharField(_('first name'), max_length=30, blank=True)
    last_name = models.CharField(_('last name'), max_length=30, blank=True)
    email = models.EmailField(_('email address'), unique=True, null=True, blank=True)
    phone_number = models.BigIntegerField(_('phone number'), unique=True, null=True, blank=True,
                                          validators=[
                                              validators.RegexValidator(r'^319[0-3,9]\d{8}$',
                                                                        ('Enter a valid mobile number.'), 'egrwfk')

                                          ],
                                          error_messages={
                                              'unique': _('A user with this mobile phone number already exits')
                                          }
                                          )
    is_staff = models.BooleanField(_('staff status'), default=False,
                                   help_text='designates whether this user can log into this admin site.')

    is_active = models.BooleanField(_('active'), default=True,
                                    help_text=_('designates whether this user should be treated as active'))

    date_joined = models.DateTimeField(_('date joined'), default=timezone.now)
    last_seen = models.DateTimeField(_('last seen date'), null=True)

    objects = UserManager()

    USERNAME_FIELD = 'username'
    REQUIRED_FIELS = ['email', 'phone_number']

    class Meta:
        db_table = 'users'
        verbose_name = _('user')
        verbose_name_plural = _('users')

    def get_full_name(self):

        full_name = '%s %s' % (self.first_name, self.last_name)
        return full_name.strip()

    def get_short_name(self):

        return self.first_name

    def email_user(self, subject, message, from_email=None, **kwargs):

        send_mail(subject, message, from_email, [self.email], **kwargs)



    @property
    def is_loggedin_user(self):

        return self.phone_number is not None or self.email is not None


    def save(self, *args, **kwargs):
        if self.email is not None and self.email.strip() == '':
            self.email = None
        super().save(*args, **kwargs)





class UserProfile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    nick_name = models.CharField(_('nick_name'), max_length=150, blank=True)
    avatar = models.ImageField(_('avatar'), blank=True)
    birthday = models.DateField(_('birthday'), null=True, blank=True)
    gender = models.NullBooleanField(_('gender'), help_text='femal is False, male is True, null is unset')
    province = models.ForeignKey(verbose_name=_('province'), to='Province', null=True, on_delete=models.SET_NULL)
    # email = models.EmailField(_('email_address'), blank=True)
    # phone_number = models.BigIntegerField(_('phone_number'), null=True, blank=True,
    #                                       validators=[
    #                                           validators=RegexValidators(r'^989[0-3,9]\d(8)$,
    #                                                                                         _('Enter a valid mobile number'), 'invalid'),
    #                                           ],
    #                                           )
    class Meta:
        db_table = 'user_profiles'
        verbose_name = _('profile')
        verbose_name_plural = _('profiles')


    @property
    def get_first_name(self):
        return self.user.first_name


    @property
    def get_last_name(self):
        return self.user.last_name


    def get_nick_name(self):
        return self.nick_name if self.nick_name else self.user.username


class  Device(models.Model):
    WEB = 1
    IOS = 2
    ANDROID = 3
    DEVICE_TYPE_CHOICES = (
        (WEB, 'web'),
        (IOS, 'ios'),
        (ANDROID, 'android')
    )

    user = models.ForeignKey(User, related_name='devices', on_delete=models.CASCADE)
    device_uuid = models.UUIDField(_('Device UUID'), null=True)
    # notify_token = models.CharField(_('notification token'), max_length=200, blank=True,
    #                                 validators=[validators.RegexValidator(r'([a-z]|[A-z]|[0-9])\w+',
    #                                                                       _('Notify token is not valid'), 'invalid')]
    #                                 )
    last_login = models.DateTimeField(choices=DEVICE_TYPE_CHOICES, default=ANDROID)
    device_type = models.PositiveSmallIntegerField(choices=DEVICE_TYPE_CHOICES, default=ANDROID)
    device_os = models.CharField(_('device os'), max_length=20, blank=True)
    device_model = models.CharField(_('device model'), max_length=50, blank=True)
    app_version = models.CharField(_('app version'), max_length=20, blank=True)
    created_time = models.DateTimeField(auto_now_add=True)

    class Meta:
        db_table = 'user_devices'
        verbose_name = _('device')
        verbose_name_plural = _('devices')
        unique_together = ('user', 'device_uuid',)


class Province(models.Model):
    name = models.CharField(max_length=50)
    is_valid = models.BooleanField(default=True)
    modified_at = models.DateTimeField(auto_now=True)
    created_at = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return self.name

do you know how I can fix the issue?

Upvotes: 0

Views: 94

Answers (1)

Rishabh Sharma
Rishabh Sharma

Reputation: 101

I think it should be:

def create_superuser(self, username, phone_number, email, password, **extra_fields):
        return self.create_user(username, phone_number, email, password, True, True, **extra_fields)

Hope it works.

Upvotes: 1

Related Questions