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
first_name,last_name, andlocationare all required parameters. If you want them optional, provide a default value as you have withpassword,is_active, etc