Reputation:
I am using Django REST Framework for my REST API.
I am creating a custom user model. Here is my code:
from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
from django.contrib.auth.models import UserManager
from django.db import models
import time
# Create your models here.
class MyUserManager(BaseUserManager):
def create_user(self, first_name, last_name, email, password, **kwargs):
if(not email):
raise ValueError("No Email Provided!")
email = self.normalize_email(email)
user = self.model(email, **kwargs)
user.set_password(password)
user.save()
return user
def create_superuser(self, first_name, last_name, email, password, **kwargs ):
kwargs.setdefault('is_staff', True)
kwargs.setdefault('is_superuser', True)
kwargs.setdefault('is_active', True)
if(kwargs.get('is_staff') is not True):
raise ValueError("Super User must be a staff in order to be in!")
if(kwargs.get('is_superuser') is not True):
raise ValueError("User must be a SuperUser!")
if(kwargs.get('is_active') is not True):
raise ValueError("Super User must be active!")
return self.create_user(first_name, last_name, email, password, **kwargs)
class MyUser(AbstractBaseUser):
first_name = models.CharField(max_length= 200, blank= False)
last_name = models.CharField(max_length= 200, blank= False)
email = models.EmailField(unique= True, blank= False)
phone_number = models.IntegerField()
company_name = models.CharField(max_length= 200, blank= False)
date_joined = models.DateTimeField(auto_now= True)
last_login = models.DateTimeField(auto_now= True, null= False)
is_staff = models.BooleanField(default= False)
is_active = models.BooleanField(default= True)
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = ['first_name', 'last_name', 'phone_number', 'company_name']
objects = MyUserManager()
Here is the Error that I am getting:
File "C:\Program Files (x86)\Python38-32\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\Desktop\Auth-App\AuthProject\AuthApplication\models.py", line 33, in create_superuser
return self.create_user(first_name, last_name, email, password, **kwargs)
File "C:\Users\Desktop\Auth-App\AuthProject\AuthApplication\models.py", line 13, in create_user
user = self.model(email, **kwargs)
File "C:\Program Files (x86)\Python38-32\lib\site-packages\django\db\models\base.py", line 500, in __init__
raise TypeError("%s() got an unexpected keyword argument '%s'" % (cls.__name__, kwarg))
TypeError: MyUser() got an unexpected keyword argument 'is_superuser'
What is a possible solution to the above problem ?
Upvotes: 1
Views: 1308
Reputation: 273
You can use AbstractBaseUser
but will also need to extend PermissionsMixin
, as is_superuser
is defined in PermissionsMixin
.
PermissionsMixin
is where the Group & Permission models are set.
Your MyUser
class should look something like this:
from django.contrib.auth.models import AbstractBaseUser, PermissionsMixin
class User(AbstractBaseUser, PermissionsMixin):
...
Here's a helpful tutorial/guide: link.
Upvotes: 1
Reputation: 334
You should inherit MyUser from AbstractUser instead from AbstractBaseUser. In fact, is_superuser
is set in PermissionMixin, used by AbstractUser. In AbstractBaseUser, this necessary var does not exists.
So the solution is simply :
from django.contrib.auth.models import AbstractUser
class MyUser(AbstractUser):
Upvotes: 1