Reputation: 427
I'am trying create my custom User model for authentication, but I, can't see the error in my code, maybe you can see and help me.
Believe me I search in whole forum before posting, even I read this post, but this is about hash password
when I try create an superuser in shell with command
c:\employee>python manage.py createsuperuser
I get the following error (complete traceback at bottom)
create_superuser() got an unexpected keyword argument 'NickName'
here is my seetings.py
#seetings.py
AUTH_USER_MODEL = 'Sinergia.Employee'
and my models.py
#models.py
# -*- coding: utf-8 -*-
from django.db import models
# Importando la configuración
from django.conf import settings
# Importando clases para los administradores
# de usuario.
from django.contrib.auth.models import AbstractBaseUser, BaseUserManager
class EmployeeManager(BaseUserManager):
def create_user(self, email, nickname, password = None):
if not email:
raise ValueError('must have an email address.')
usuario = self.model\
(
Email = self.normalize_email(email),
NickName = nickname,
)
usuario.set_password(password)
usuario.save(using = self._db)
return usuario
def create_superuser(self, email, nickname, password):
usuario = self.create_user\
(
email = email,
nickname = nickname,
password = password,
)
usuario.is_admin = True
usuario.save(using = self._db)
return usuario
class Employee(AbstractBaseUser):
Email = models.EmailField(max_length = 254, unique = True)
NickName = models.CharField(max_length = 40, unique = True)
FBAccount = models.CharField(max_length = 300)
# Estados del Usuario.
is_active = models.BooleanField(default = True)
is_admin = models.BooleanField(default = False)
object = EmployeeManager()
# Identificador Único del Usuario.
USERNAME_FIELD = 'Email'
# Campos obligatorios.
REQUIRED_FIELDS = ['NickName']
def get_full_name(self):
return self.Email
def get_short_name(self):
return self.NickName
def __unicode__(self):
return self.Email
def has_perm(self, perm, obj=None):
"Does the user have a specific permission?"
# Simplest possible answer: Yes, always
return True
def has_module_perms(self, app_label):
"Does the user have permissions to view the app `app_label`?"
# Simplest possible answer: Yes, always
return True
@property
def is_staff(self):
"Is the user a member of staff?"
# Simplest possible answer: All admins are staff
return self.is_admin
class Article(models.Model):
Author = models.ForeignKey(settings.AUTH_USER_MODEL)
Upvotes: 4
Views: 9148
Reputation: 501
What my problem solved is the addition of **extra_fields
like this:
def create_superuser(self, email, password,**extra_fields):
"""
Creates and saves a superuser with the given email, and password.
"""
user = self.create_user(email,
password=password,**extra_fields
)
user.is_admin = True
user.save()
return user
May it also solve your problem
Upvotes: 2
Reputation: 146
1- You should inherit from PermissionMixin
in the Employee
class Employee(AbstractBaseUser, PermissionsMixin):
.....
Because of the USERNAME_FIELD Exist on PermissionMixin class so you can override it.
2- objects not object
Upvotes: 0
Reputation: 165172
You are using CamelCase
for your field names, it is not a good practice, and it's causing the errors.
Hence, your function fails when you call (in create_superuser()
):
self.create_user(email = email, nickname = nickname, password = password)
Either call:
self.create_user(Email = email, NickName = nickname, password = password)
Or alternatively make all your field names lowercase
.
Upvotes: 1
Reputation: 599450
You have a typo when you assign the manager:
class Employee(AbstractBaseUser):
...
objects = EmployeeManager()
objects, not object
.
Upvotes: 3