Reputation: 658
I've decided to create a custom user model for an app by extending Django's AbstractBaseUser along with the PermissionsMixin class. For some reason when I run syncdb or sqall, the is_staff
field on my model is omitted from the query. As you know, the is_staff
field is required to be able to access the Admin interface. Here's my code snippet(Django 1.5.8):
from django.contrib.auth.models import ( \
AbstractBaseUser, BaseUserManager, PermissionsMixin)
#models.py snippet
class FooUser(AbstractBaseUser, PermissionsMixin):
#foo fields here...
is_active = models.BooleanField(default=False, verbose_name='account is activated')
is_staff = models.BooleanField(default=False, verbose_name='staff account')
I've looked at the source code of the two classes and neither of them implement the is_staff
field so I'm not sure what the issue is. The only reason why I'm using AbstractBaseUser instead of AbstractUser(which has the is_staff
field) is because I don't need or want the first, last name fields in my user model. Adding other fields such as is_admin
worked just fine but this particular field just won't budge.
Upvotes: 1
Views: 1183
Reputation: 658
The following code was the cause of the omission:
@property
def is_staff(self):
return self.is_admin
By commenting it out, the issue was fixed. Alternatively you can also do this:
#models.py snippet
class FooUser(AbstractBaseUser, PermissionsMixin):
#foo fields here...
is_active = models.BooleanField(default=False, verbose_name='account is activated')
is_admin = models.BooleanField(default=False, verbose_name='staff account')
@property
def is_staff(self):
"Is the user a member of staff?"
# Simplest possible answer: All admins are staff
return self.is_admin
Full example: https://docs.djangoproject.com/en/1.5/topics/auth/customizing/#a-full-example
Upvotes: 1