Reputation: 201
I am working with user authentication system where I created a user registration model using AbstractBaseUser. then I create super User using terminal . But when I go the admin and write email and password there . It gives me the error that: 'User' object has no attribute 'is_staff'
My models.py file is:
from django.db import models
from django.contrib.auth.models import AbstractBaseUser,BaseUserManager
SUBJECT_CHOICES = (
('math','Math'),
('physics','Physics'),
('chemistry','Chemistry'),
)
class UserManager(BaseUserManager):
def create_user(self, email, full_name=None, password=None, is_staff=False, is_admin=False):
if not email:
raise ValueError("User must have an email")
if not password:
raise ValueError("User must have a password")
user_obj = self.model(email=self.normalize_email(email), full_name=full_name)
user_obj.set_password(password)
user_obj.staff = is_staff
user_obj.admin = is_admin
user_obj.save(using=self._db)
return user_obj
def create_staffuser(self,email,full_name=None,password=None):
user = self.create_user(email, full_name=full_name,password=password)
return user
def create_superuser(self,email, full_name=None,password=None):
user = self.create_user(email, full_name=full_name, password=password)
return user
class User(AbstractBaseUser):
full_name = models.CharField(max_length=255, blank=True, null=True)
sur_name = models.CharField(max_length=255, blank=True, null=True)
email = models.EmailField(max_length=255 ,unique=True)
choose_subject = models.CharField(choices=SUBJECT_CHOICES , max_length=100)
staff = models.BooleanField(default=False)
admin = models.BooleanField(default=False)
time_stamp = models.DateTimeField(auto_now_add=True)
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = []
object = UserManager()
def __str__(self):
return self.full_name
my forms.py file is:
class RegisterForm(forms.ModelForm): # A form for creation new user included all all the required fields including repeated password
password1 = forms.CharField(label='Enter Password' , widget=forms.PasswordInput )
password2 = forms.CharField(label='Password confirmation', widget=forms.PasswordInput)
class Meta:
model = User
fields=('full_name','sur_name','email','choose_subject')
def clean_password2(self): # check that two password entries match or not
password1 = self.cleaned_data.get("password1")
password2 = self.cleaned_data.get("password2")
if password1 and password2 and password1 != password2:
raise forms.ValidationError("password dont match")
return password2
def save(self , commit=True): #save the provided password in hashed format
user = super(RegisterForm , self).save(commit=False)
user.set_password(self.cleaned_data["password1"])
#user.active = False #send confirmation email
if commit:
user.save()
return user
my views.py is:
class RegisterView(CreateView):
form_class = RegisterForm
template_name = "users/register.html"
success_url = '/register'
Thank You.
Upvotes: 2
Views: 3031
Reputation: 778
It should be is_staff but not staff... You can have it working if you type staff or change the model field to is_staff.
Upvotes: 0
Reputation: 1062
There are two ways to resolve this problem
Number one: create property method into your user model:
@property
def is_staff(self):
return self.staff
Number two: rename "staff" field to "is_staff", it would be righter
Upvotes: 2