python-3.xdjangodjango-modelsdjango-rest-framework

django.core.exceptions.ImproperlyConfigured: AUTH_USER_MODEL refers to model 'api.User' that has not been installed


I just create Django project and get this error

django.core.exceptions.ImproperlyConfigured: AUTH_USER_MODEL refers to model 'api.User' that has not been installed

this my files

settings

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    "rest_framework",
    "api",
]

AUTH_USER_MODEL = "api.User"

models


from django.db import models
from django.contrib.auth.models import AbstractUser
from django.contrib.auth import get_user_model
class User(AbstractUser):
    ACTIVE = 0
    DEACTIVE = 1
    STATUS = [
        (ACTIVE,"active"),
        (DEACTIVE,"deActive")
    ]
    
    chatID = models.PositiveBigIntegerField()
    fname = models.CharField(max_length=20)
    lname = models.CharField(max_length=20)
    invitedBy = models.ForeignKey(to=get_user_model(),on_delete=models.PROTECT,default=None)
    point = models.PositiveBigIntegerField(default=0)
    team = models.ForeignKey(to=TelegramGroup,on_delete=models.CASCADE)
    custom_status = models.PositiveSmallIntegerField(choices=STATUS,default=0)
    
    def __str__(self) -> str:
        return self.chatID

When I delete all the fields and just set user table like this

from django.db import models
from django.contrib.auth.models import AbstractUser

class User(AbstractUser):
    pass

I get no errors


Solution

  • The invitedBy field is referencing the same model, so you have to declare it like this:

    invitedBy = models.ForeignKey('self',on_delete=models.PROTECT,default=None,null=True)