I am trying to createsuper user when I entered email and password I got this error
File "/mnt/c/Users/ZAKARIA/Desktop/project/env/lib/python3.8/site-packages/django/contrib/auth/management/commands/createsuperuser.py", line 232, in handle
self.UserModel._default_manager.db_manager(database).create_superuser(
TypeError: create_superuser() missing 2 required positional arguments: 'first_name' and 'last_name'
my class managers
from django.contrib.auth.base_user import BaseUserManager
class UserManger(BaseUserManager):
def create_user(self, email, first_name, last_name, password=None):
if not email:
raise ValueError("Users must have an email address")
if not first_name or last_name:
raise ValueError("Users must have a first and last name")
user = self.model(
email=self.normalize_email(email),
first_name=first_name,
last_name=last_name,
)
user.set_password(password)
user.save(using=self._db)
return user
def create_superuser(self, email,password, first_name, last_name):
user = self.create_user(
email=self.normalize_email(email),
password=password,
first_name=first_name,
last_name=last_name,
)
user.set_password(user.password)
#user.is_staff = True
user.is_superuser = True
#user.is_admin = True
user.save(using=self._db)
return user
models
import datetime
from django.core.mail import send_mail
from distutils.command.upload import upload
from django.db import models
from django.contrib.auth.base_user import AbstractBaseUser
from django.contrib.auth.models import PermissionsMixin
from django.utils import timezone
from phonenumber_field.modelfields import PhoneNumberField
from .managers import UserManger
GENDER_MALE = "m"
GENDER_FEMALE = "f"
OTHER = "o"
GENDER_CHOICES = (
(GENDER_MALE, "Male"),
(GENDER_FEMALE, "Female"),
(OTHER, "Other"),
)
class User(AbstractBaseUser):
email = models.EmailField(
verbose_name='email address',
max_length=255,
unique=True,
)
date_of_birth = models.DateField()
first_name = models.CharField(max_length=30, blank=True)
last_name = models.CharField(max_length=30, blank=True)
gender = models.CharField(max_length=1, choices=GENDER_CHOICES, blank=True)
picture = models.ImageField(
upload_to='images/users', null=True, verbose_name="")
is_active = models.BooleanField(default=True)
#is_staff = models.BooleanField(default=False)
phone = PhoneNumberField()
is_admin = models.BooleanField(default=False)
#credits =models.PositiveIntegerField(default=100)
linkedin_token = models.TextField(blank=True, default='')
expiry_date = models.DateTimeField(null=True, blank=True)
objects = UserManger()
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = ['first_name' , 'last_name']
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 __str__(self):
return self.email
def has_perm(self, prem, obj=None):
"Does the user have a specific permission?"
return True
def has_module_perm(self, app_label):
"Does the user have permissions to view the app `app_label`?"
return True
''''
@property
def is_staff(self):
"Is the user a member of staff"
return self.is_admin'''
"""@property
def is_out_of_credits(self):
"Is the user out of credits"
return self.credits > 0
@property
def has_sufficient_credits(self,cost):
return self.credits - cost >= 0
"""
@property
def linkedin_signed_in(self):
return bool(self.linkedin_token) and self.expiry_date > timezone.now()
I have been seeing a lot of questions similar to this but i still don't understand the answers given, some say add positional arguements, I don't quite understand
can any one help me to solve this problem ?
CodePudding user response:
That's probably because you have some extra fields in the UserModel that are not passing accurately in the create_superuser
.
Try **extra_fields
arg instead of passing first_name, last_name separately, as the docs about Manager methods suggest,
The extra_fields keyword arguments are passed through to the User’s init method to allow setting arbitrary fields on a custom user model.
So, modify your UserManger
class according to this and then this will rectify the problem.
Modified Code:
class UserManger(BaseUserManager):
"""Define a model manager for User model"""
def _create_user(self, email, password=None, **extra_fields):
"""Create and save a User with the given email and password."""
if not email:
raise ValueError('The given email must be set')
user = self.model(email=email, **extra_fields)
user.set_password(password)
user.save(using=self._db)
return user
def create_user(self, email, password=None, **extra_fields):
extra_fields.setdefault('is_staff', False)
extra_fields.setdefault('is_superuser', False)
return self._create_user(email, password, **extra_fields)
def create_superuser(self, email, password=None, **extra_fields):
"""Create and save a SuperUser with the given email and password."""
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(email, password, **extra_fields)
Now you can set first_name
, last_name
, as REQUIRED_FIELDS
in the UserModel
, like this.
REQUIRED_FIELDS = ['first_name' , 'last_name']
For detailed information on BaseUserManager
, you can also check Docs
Edit: After seeing your UserModel, this would be better the use AbstractUser instead of AbstractBaseUser, AbstractUser will carry all the fields that a default Django User has. So you don't have to define the fields like first_name, last_name, is_staff or etc...
That is how you can modify it
# import
from django.contrib.auth.models import AbstractUser
# Model
class User(AbstractUser):
username = None # as you are using email as username
email = models.EmailField(
verbose_name='email address',
max_length=255,
unique=True,
)
date_of_birth = models.DateField()
gender = models.CharField(max_length=1, choices=GENDER_CHOICES, blank=True)
picture = models.ImageField(
upload_to='images/users', null=True, verbose_name="")
phone = PhoneNumberField()
is_admin = models.BooleanField(default=False)
#credits =models.PositiveIntegerField(default=100)
linkedin_token = models.TextField(blank=True, default='')
expiry_date = models.DateTimeField(null=True, blank=True)
objects = UserManger()
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = ['first_name' , 'last_name']
# Do anything else here