Home > OS >  How to inherent from a class that is already inherited from AbstractBaseUser in Django
How to inherent from a class that is already inherited from AbstractBaseUser in Django

Time:03-27

I am trying to make an inheritance from a User class that is already inherited from the AbstractBaseUser but I am receiving an error that is telling me AUTH_USER_MODEL refers to model 'authentication.User' that has not been installed and I am sure I added the AUTH_USER_MODEL = 'authentication.User' in settings.py, I wonder how can I superpass this error and successfully inherent from the User Class this is the code:

from time import timezone

from django.contrib.auth.base_user import BaseUserManager, AbstractBaseUser
from django.contrib.auth.models import PermissionsMixin
from django.db import models


class ResidentType(models.TextChoices):
    Family = ('FM', 'Family')
    Individual = ('IV', 'Individual')


class UserManager(BaseUserManager):
    def create_user(self, username, first_name, last_name, email, password=None):
        if username is None:
            raise TypeError('User must have a username')
        if email is None:
            raise TypeError('User must have an Email')
        if first_name is None:
            raise TypeError('User must have a first_name')
        if last_name is None:
            raise TypeError('User must have a last_name')
        user = self.model(username=username, first_name=first_name, last_name=last_name,
                          email=self.normalize_email(email), )
        user.set_password(password)
        user.save()
        return user

    def create_admin(self, username, first_name, last_name, email, password=None):
        if username is None:
            raise TypeError('User must have a username')
        if email is None:
            raise TypeError('User must have an Email')
        if first_name is None:
            raise TypeError('User must have a first_name')
        if last_name is None:
            raise TypeError('User must have a last_name')
        admin = self.model(username=username, first_name=first_name, last_name=last_name,
                          email=self.normalize_email(email), )
        admin.set_password(password)
        Admin.admin = True
        Admin.is_active = True
        admin.save()
        return admin

    def create_syndic(self, username, first_name, last_name, email):
        password = User.objects.make_random_password()
        if username is None:
            raise TypeError('User must have a username')
        if email is None:
            raise TypeError('User must have an Email')
        if first_name is None:
            raise TypeError('User must have a first_name')
        if last_name is None:
            raise TypeError('User must have a last_name')
        syndic = self.model(username=username, first_name=first_name, last_name=last_name,
                          email=self.normalize_email(email), )
        syndic.set_password(password)
        Syndic.admin = True
        Syndic.staff = True
        Syndic.is_active = True
        syndic.save()
        return

    def create_resident(self, username, first_name, last_name, email, password=None):
        if username is None:
            raise TypeError('User must have a username')
        if email is None:
            raise TypeError('User must have an Email')
        if first_name is None:
            raise TypeError('User must have a first_name')
        if last_name is None:
            raise TypeError('User must have a last_name')
        resident = self.model(username=username, first_name=first_name, last_name=last_name,
                          email=self.normalize_email(email), )
        resident.set_password(password)
        Resident.admin = False
        Resident.staff = False
        Resident.is_active = False
        resident.save()
        return resident


class User(AbstractBaseUser, PermissionsMixin):
    username = models.CharField(max_length=255, unique=True, db_index=True)
    first_name = models.CharField(max_length=255, db_index=True)
    last_name = models.CharField(max_length=255, db_index=True)
    email = models.EmailField(max_length=255, unique=True, db_index=True)
    password = models.CharField(max_length=255, db_index=True)
    is_active = models.BooleanField(default=False)
    staff = models.BooleanField(default=False)
    admin = models.BooleanField(default=False)
    objects = UserManager()

    def __str__(self):
        return self.email

    def has_module_perms(self, app_label):
        return True

    USERNAME_FIELD = 'email'
    REQUIRED_FIELDS = []

    def get_full_name(self):
        return self.email

    def get_short_name(self):
        return self.email

    def has_perm(self, perm, obj=None):
        return True

    @property
    def is_staff(self):
        return self.staff

    @property
    def is_admin(self):
        return self.admin


    class Meta:
        abstract = True



class Admin(User):
    pass


class Syndic(User):
    admin = models.ForeignKey(Admin, on_delete=models.CASCADE)


class Resident(User):
    pass
    type = models.CharField(max_length=2, choices=ResidentType, default=ResidentType.Family)
    syndic = models.ForeignKey(Syndic, on_delete=models.CASCADE)

CodePudding user response:

I believe the problem is with making your custom User model an abstract model.

Please recheck, verify and confirm.

CodePudding user response:

Can't comment because of my reputation, sorry

  • have you migrated your custom user model?
  • Also, Abstract models can not be registered inside Admin, they can't stand alone, if you want to use the User model over the default one, remove abstract = True
  • Related