UserManager creation for AbstractUserModel extended MyUser

233 views
Skip to first unread message

Vishesh Mangla

unread,
Jan 9, 2021, 8:05:34 AM1/9/21
to Django users


(venv) PS C:\Users\Dell\OneDrive\Desktop\djangodev\mass> python manage.py createsuperuser
PAN ID: 6785
Email: s@g
Error: Enter a valid email address.
Email: s...@gmail.com
Name: fjghg
Type: fg
Error: Value 'fg' is not a valid choice.
Type: ADM
Holding: 568
Password:
Password (again):
This password is too short. It must contain at least 8 characters.
Bypass password validation and create user anyway? [y/N]: y
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 "C:\Users\Dell\OneDrive\Desktop\djangodev\venv\lib\site-packages\django\core\management\__init__.py", line 401, in execute_from_command_line
    utility.execute()
  File "C:\Users\Dell\OneDrive\Desktop\djangodev\venv\lib\site-packages\django\core\management\__init__.py", line 395, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:\Users\Dell\OneDrive\Desktop\djangodev\venv\lib\site-packages\django\core\management\base.py", line 330, in run_from_argv
    self.execute(*args, **cmd_options)
  File "C:\Users\Dell\OneDrive\Desktop\djangodev\venv\lib\site-packages\django\contrib\auth\management\commands\createsuperuser.py", line 79, in execute
    return super().execute(*args, **options)
  File "C:\Users\Dell\OneDrive\Desktop\djangodev\venv\lib\site-packages\django\core\management\base.py", line 371, in execute
    output = self.handle(*args, **options)
  File "C:\Users\Dell\OneDrive\Desktop\djangodev\venv\lib\site-packages\django\contrib\auth\management\commands\createsuperuser.py", line 189, in handle
    self.UserModel._default_manager.db_manager(database).create_superuser(**user_data)
  File "C:\Users\Dell\OneDrive\Desktop\djangodev\mass\base\models.py", line 25, in create_superuser
    return super()._create_user(PAN_ID,  password, **extra_fields)
AttributeError: 'super' object has no attribute '_create_user'

Vishesh Mangla

unread,
Jan 9, 2021, 8:08:56 AM1/9/21
to Django users
Hi, please help me create this UserManager child class. I 'm stuck up on this. I also need to divide my users into three groups. 
For this I extended the migrations file.   https://dpaste.org/9VSd. But running `makemigrations` gives no changes detected.
 Please help with this issue too.
Thanks in advance.

Vishesh Mangla

unread,
Jan 9, 2021, 8:18:52 AM1/9/21
to Django users
Another thing is that my "employees" group has an additional CharField requirement. Only the employees group.How can I add specifically a field to that group? Something this this:https://stackoverflow.com/questions/16407217/add-extra-fields-to-django-to-django-auth-groups

Amitesh Sahay

unread,
Jan 9, 2021, 8:41:23 AM1/9/21
to Vishesh Mangla, Django users
Could you please paste your models.py


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/b4ff8445-8ed7-4d29-8ae6-73f3b1f8295fn%40googlegroups.com.

Vishesh Mangla

unread,
Jan 9, 2021, 9:08:24 AM1/9/21
to Django users
I did paste my `models.py` with the help the help of a dpaste link.
```
class UserManagerBaseUserManager):
    
    def _create_user(selfPAN_IDpassword=None, **extra_fields):
        """
        Creates and saves a User with the given email, date of
        birth and password.
        """
        if not PAN_ID:
            raise ValueError('Users must have an email address')
        extra_fields['email'] = self.normalize_email(extra_fields["email"])
        user = self.model(PAN_ID=PAN_ID, **extra_fields)

        user.set_password(password)
        user.save(using=self._db)
        return user
    
    def create_user(selfPAN_IDpassword=None, **extra_fields):

        extra_fields.setdefault('is_staff'False)
        extra_fields.setdefault('is_superuser'False)
        return self._create_user(PAN_ID, password, **extra_fields)
    
    def create_superuser(selfPAN_IDpassword=None, **extra_fields):
        extra_fields.setdefault('is_staff'True)
        extra_fields.setdefault('is_superuser'True)

        if extra_fields.get('is_staff'is not True:
            raise ValueError('Superuser must have is_staff=True.')
        if extra_fields.get('is_superuser'is not True:
            raise ValueError('Superuser must have is_superuser=True.')

        return self._create_user(PAN_ID, password, **extra_fields)


```   The following code worked. I used https://simpleisbetterthancomplex.com/tutorial/2016/07/22/how-to-extend-django-user-model.html and the source code from https://github.com/django/django/blob/master/django/contrib/auth/models.py.   Can I get help in the migrations and creating groups? Code is in the dpaste s.

Amitesh Sahay

unread,
Jan 9, 2021, 9:30:55 AM1/9/21
to Django users
Hello Vishesh, 

Below is from my models.py:

models.py
-------------
from django.contrib.auth.base_user import BaseUserManager
from django.contrib.auth.models import AbstractBaseUser, PermissionsMixin
from django.utils.translation import gettext_lazy as _


class AccountManager(BaseUserManager):
def create_superuser(self, email, password, **extra_fields):

extra_fields.setdefault('is_staff', True)
extra_fields.setdefault('is_superuser', True)
        extra_fields.setdefault('is_active', True)


if extra_fields.get('is_staff') is not True:
            raise ValueError(_('Superuser must have is_staff=True.'))

if extra_fields.get('is_superuser') is not True:
            raise ValueError(_('Superuser must have is_superuser=True.'))
return self.create_user(email, password, **extra_fields)

def create_user(self, email, password, **extra_fields):
if not email:
raise ValueError(_('Enter the email before proceeding'))

email = self.normalize_email(email)
user = self.model(email=email, password=password, **extra_fields)
user.set_password(password)
user.save()
return user


class Shop(AbstractBaseUser, PermissionsMixin):
email = models.EmailField(unique=True)
shop_name = models.CharField(max_length=150)
contact = models.CharField(max_length=10)
State = models.CharField(max_length=100)
district = models.CharField(max_length=100)
location = models.CharField(max_length=100)
is_staff = models.BooleanField(default=False)
is_active = models.BooleanField(default=True)
date_joined = models.DateTimeField(auto_now_add=True)
last_login = models.DateTimeField(null=True)

objects = AccountManager()

USERNAME_FIELD = 'email'
REQUIRED_FIELDS = ['shop_name', 'contact', 'is_staff']

def __str__(self):
return str(self.shop_name)

See if that helps you out.

Regards,
Amitesh


Vishesh Mangla

unread,
Jan 9, 2021, 9:37:23 AM1/9/21
to Django users
Oops I guess, my bad messaging style made you miss the last few pieces of my messages. Thanks, but that problem is resolved as written in my previous message with the working code. Now I require help with the Migrations problem.

Amitesh Sahay

unread,
Jan 9, 2021, 9:53:27 AM1/9/21
to Django users
Did you happen to remove the private method "def _create_user" from your models.py?

Regards,
Amitesh 


Reply all
Reply to author
Forward
0 new messages