pythondjangodjango-3.1

Ignore `is_active` filed for custom user model


I'm trying to make custom user model, when instead of few boolean columns, I will manage user permissions based on one status filed:

from django.db import models
from django.contrib.auth.models import AbstractUser
from django.utils.translation import ugettext_lazy as _


class MyUser(AbstractUser):
    username = None
    is_staff = None
    is_superuser = None
    is_active = None
    email = models.EmailField(_('email address'), unique=True)
    status = models.PositiveSmallIntegerField(default=0)

In settings.py file added:

AUTHENTICATION_BACKENDS = ['django.contrib.auth.backends.AllowAllUsersModelBackend']

Then, when trying to login, getting error: This account is inactive.

How to ignore is_active filed for my custom user model ?


Solution

  • You can replace is_active with a property, so:

    class MyUser(AbstractUser):
        # …
        # no is_active field
        # …
        @property
        def is_active(self):
            return self.status > 0