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)
Advertisement
Answer
For anyone who will encounter this later, I have solved this issue by using proxy models rather than using the abstract classes, you can also make a one to one field from the User class and use it for the other classes or you can simply create a role attribute in the User model rather than making other models for it and keep tracking for the roles in each view you make.