djangodjango-authentication

AttributeError: 'Account' object has no attribute 'is_staff'


I have customized user model, Here is the model:

from django.db import models
from django.contrib.auth.models import AbstractBaseUser
from django.contrib.auth.models import BaseUserManager


class AccountManager(BaseUserManager):

    def create_user(self, username=None, password=None, **kwargs):
        account = self.model(username=username)

        account.set_password(password)
        account.is_stuff = False
        account.save()

        return account

    def create_superuser(self, username, password, **kwargs):
        account = self.create_user(username, password, **kwargs)
        account.is_admin = True
        account.is_staff = True
        account.save()

        return account


class Account(AbstractBaseUser):
    username = models.CharField(max_length=40, unique=True)
    is_admin = models.BooleanField(default=False)
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)
    initialized = models.BooleanField(default=False)

    USERNAME_FIELD = 'username'

    objects = AccountManager()

then when I try to get list of all users it throws AttributeError: 'Account' object has no attribute 'is_staff' .

class GetUserList(ListAPIView):
    permission_classes = (permissions.IsAdminUser,)
    queryset = Account.objects.all()
    serializer_class = AccountSerializer

Solution

  • IsAdminUser calls is_staff which is defined on django's auth user model. So if you have your own custom user model, you need to provide an implementation to this also

    In django's AbstractUser class, it is a BooleanField, as you can see here