Reputation: 33
from django.db import models
from django.contrib.auth.models import AbstractBaseUser, BaseUserManager
# Create your models here.
class MyAccountManager(BaseUserManager):
def create_user(self, email, username, password=None):
if not email:
raise ValueError('user must have smartcard')
if not username:
raise ValueError('must have username')
user = self.create_user(
email=self.normalize_email(email),
username=username,
)
user.set_password(password)
user.save(using=self._db)
return user
def create_superuser(self, email, username, password):
user = self.create_user(
email=self.normalize_email(email),
password=password,
username=username,
)
user.is_admin =True
user.is_staff =True
user.is_superuser=True
user.save(using=self._db)
return user
class Account(AbstractBaseUser):
username = models.CharField(max_length=100, unique=True)
email = models.EmailField(verbose_name="email", max_length=30, unique=True)
department = models.CharField(max_length=30, choices=DEPARTMENT)
USERNAME_FIELD = 'email'
REQUIRED_FIELD = ['username',]
objects=MyAccountManager()
def __str__(self):
return self.email
def has_perm(self, perm, obj=None):
return self.is_admin
def has_module_perms(self, app_label):
return True
I just can't get what is wrong.
While creating a superuser the prompt doesnt ask me for entering username
I modified the settings.py file as well. Tried several times with various databases. I have also added the username in REQUIRED_FIELDS section.
Upvotes: 3
Views: 6193
Reputation: 11
Works perfectly after adding the REQUIRED_FIELDS = ['username'] while using the CustomUser model in django .I am able to create the superuser using the terminal
from django.db import models
from django.contrib.auth.models import AbstractUser
class User(AbstractUser):
name = models.CharField(max_length=200, null=True)
email = models.EmailField(unique=True, null=True)
bio = models.TextField(null=True)
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = ['username']
def __str__(self):
return self.email
Upvotes: 1
Reputation: 1105
@Omkar Kale, Please look your method:
def create_superuser(self, email, username, password):
user = self.create_user(
email=self.normalize_email(email),
password=password,
username=username,
)
Please change it:
def create_superuser(self, username, email, password):
user = self.create_user(
username=username,
email=self.normalize_email(email),
password=password,
)
Look, you need to pass value according to the parameter. So
Then you will no longer going to receive error for positional argument. you have to pass them according to the position of the parameter.
Upvotes: 2
Reputation: 960
Welcome to SO, @Omkar.
You just need to change the fields REQUIRED_FIELD
to REQUIRED_FIELDS
.
REQUIRED_FIELDS = ['username']
You can check it on the docs.
Upvotes: 2