K
K
kvarel2022-04-09 23:24:00
Django
kvarel, 2022-04-09 23:24:00

AUTH_USER_MODEL refers to model 'employee.Employee' that has not been installed?

Help me figure out what's wrong?

employee/models.py file

from django.db import models
from django.contrib.auth.backends import ModelBackend
from django.contrib.auth.models import AbstractBaseUser, BaseUserManager


DEP_CHOICES = [
    ('HR', 'Отдел кадров'),
    ('BUH', 'Бухгалтерия'),
    ('SALE', 'Продажи'),
    ('SEO', 'Руководство')
]


class EmpManager(BaseUserManager):

    def create_user(self, department, fio, email, password=None, **other_fields):
        if not department or not fio or not email or not password:
            raise ValueError('Недостаточно данных для регистрации сотрудника')
        email = self.normalize_email(email)
        user = Employee(department=department, fio=fio, email=email, **other_fields)
        user.set_password(password)
        user.save()
        return user

    def create_superuser(self, department, fio, email, password=None):
        return self.create_user(department, fio, email, password, is_staff=True, is_superuser=True)


class Employee(AbstractBaseUser):
    tabel = models.BigAutoField('Табельный номер', primary_key=True)
    department = models.CharField('Отдел', choices=DEP_CHOICES, max_length=50)
    email = models.EmailField('Почта', max_length=50)
    fio = models.CharField('ФИО', max_length=100)
    is_staff = models.BooleanField(default=False)
    is_superuser = models.BooleanField(default=False)

    USERNAME_FIELD = 'tabel'
    EMAIL_FIELD = 'email'
    REQUIRED_FIELDS = ['department', 'fio', 'email']
    objects = EmpManager()


class EmpBackend(ModelBackend):

    def authenticate(self, request, tabel=None, password=None, department=None, **kwargs):
        if tabel is None or department is None or password is None:
            return
        user = Employee.objects.filter(tabel=tabel, department=department)
        if user and user.get().check_password(password):
            return user
        return

    def get_user(self, user_id):
        user = Employee.objects.filter(tabel=user_id)
        if user:
            return user.get()
        return


data from settings.py file
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'employee',
]
AUTH_USER_MODEL = 'employee.Employee'
AUTHENTICATION_BACKENDS = ['employee.models.EmpBackend']


why is it giving an error? Trying to do makemigrations for the first time in a project

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
kvarel, 2022-04-10
@kvarel

If anyone encounters such a problem, do not import ModelBackend into the models.py file.
It turns out that everything was because of him.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question