1

Got the following error when I try to create new superuser using terminal.

TypeError: create_superuser() missing 3 required positional arguments: 'first_name', 'last_name', and 'location' 

I followed another stackoverflow page , in comments below. In that page it insisted to create a create_superuser function with user.save(using=self._db) Still this code have bugs,

How to use 'first_name', 'last_name', and 'location' without providing any default values to them.

first_name , last_name and location are CharField with max_length = 30 and it can have blank values.

In this custom-user-model the username is replaced with mobile_no by making it as unique in the User class.

models.py

from django.db import models from django.contrib.auth.models import ( AbstractBaseUser, BaseUserManager,PermissionsMixin ) class UserManager(BaseUserManager): # All required field must be passed below as argument def create_user(self, mobile_no, role, email, first_name, last_name, location, password=None, is_active=True,is_staff=False, is_admin= False): if not mobile_no: raise ValueError("User must have an Mobile number as username ") if not password: raise ValueError("Users must have a password") user_obj= self.model( mobile_no= mobile_no ) user_obj.email = email user_obj.set_password(password) user_obj.role = role user_obj.first_name = first_name user_obj.las_name = last_name user_obj.is_active = is_active user_obj.location = location user_obj.is_staff = is_staff user_obj.save(using=self._db) return user_obj def create_superuser(self, mobile_no, role, email, first_name, last_name, location, password=None, is_active=True,is_staff=False, is_admin= False): user_obj= self.model( mobile_no=mobile_no ) user_obj.email = email user_obj.set_password(password) user_obj.role = role user_obj.first_name = first_name user_obj.las_name = last_name user_obj.location = location user_obj.is_staff = True user_obj.is_admin = True user_obj.is_active = is_active user_obj.save(using=self._db) return user_obj def create_staffuser(self, mobile_no, role, email, first_name, last_name, location, password=None, is_active=True,is_staff=False, is_admin= False): user_obj= self.model( mobile_no=mobile_no ) user_obj.email = email user_obj.set_password(password) user_obj.role = role user_obj.first_name = first_name user_obj.las_name = last_name user_obj.location = location user_obj.is_staff = True user_obj.is_admin = False user_obj.is_active = is_active user_obj.save(using=self._db) return user_obj # def get_by_natural_key(self, mobile_no_): # print(mobile_no_) # return self.get(mobile_no=mobile_no_) class User(AbstractBaseUser, PermissionsMixin): mobile_no = models.CharField(max_length=10,unique=True) email = models.EmailField(max_length=255) role = models.CharField(max_length=255) first_name = models.CharField(max_length=30, blank=True) last_name = models.CharField(max_length=30, blank=True) location = models.CharField(max_length=30, blank=True) date_joined = models.DateTimeField(auto_now_add=True) is_active = models.BooleanField(default=True) is_staff = models.BooleanField(default=False) is_admin = models.BooleanField(default=False) USERNAME_FIELD = 'mobile_no' REQUIRED_FIELDS = ['email', 'role'] objects= UserManager() def get_full_name(self): return self.first_name + self.last_name def get_short_name(self): return self.mobile_no def __str__(self): return self.mobile_no @property def is_staff(self): return self.staff @property def is_admin(self): return self.admin @property def is_active(self): return self.active 
3

2 Answers 2

5

This code works for the above case

class UserManager(BaseUserManager): def create_user(self, mobile_no, role, password=None): """ Creates and saves a User with the given email and password. """ if not mobile_no: raise ValueError('Users must have an Mobile No') user = self.model( mobile_no=mobile_no, ) user.set_password(password) user.activated=True user.save(using=self._db) return user def create_superuser(self, mobile_no, role, password): """ Creates and saves a superuser with the given email and password. """ user = self.create_user(mobile_no, role=role, password=password ) user.is_admin = True user.activated=True user.save(using=self._db) return user def create_staffuser(self, mobile_no, role, password): """ Creates and saves a staffuser with the given email and password. """ user = self.create_user(mobile_no, role=role, password=password ) user.is_staff = True user.activated=True user.save(using=self._db) return user class Users(AbstractBaseUser, PermissionsMixin): object = UserManager() mobile_no = models.IntegerField(_('MobNumber'), null=True, blank=True,unique=True) email = models.EmailField(_('Email'), max_length=75, null=False, blank=False) first_name = models.CharField(_('FirstName'), max_length=50, null=True, blank=True) last_name = models.CharField(_('LastName'), max_length=70, null=True, blank=True) role = models.CharField(_('Role'), max_length=70, null=True, blank=True) location = models.CharField(_('Location'), max_length=70, null=True, blank=True) date_time = models.DateTimeField(_('DateTime'), auto_now=True, null=True, blank=True) activated = models.BooleanField(_('Activated'), default=False) is_admin = models.BooleanField(_('is_admin'), default=False) is_staff = models.BooleanField(_('is_staff'), default=False) def __unicode__(self): return str(self.mobile_no) def __str__(self): return str(self.mobile_no) def get_full_name(self): return self.first_name + " " + self.last_name class Meta: ordering = ['-id'] @property def is_staff(self): return self.is_admin def has_perm(self, perm, obj=None): return self.is_admin def has_module_perms(self, app_label): return self.is_admin USERNAME_FIELD = 'mobile_no' REQUIRED_FIELDS = ['role'] 
Sign up to request clarification or add additional context in comments.

Comments

0

You are probably having this issue because in your required fields, you only specified email and role without the first_name, last_name, and location. When declaring the user user manager, you did not supply default values for those fields, so you have to specify them in the required field. That should solve your problem.

The credit for this answer goes to @Daniel Roseman: Issue with createsuperuser when implementing custom user model

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.