AUTH_USER_MODEL refers to model 'accounts.User' that has not been installed

前端 未结 5 1897
渐次进展
渐次进展 2021-01-05 09:42

I\'m using a custom user model, extended with AbstractUser. Here\'s my models.py:

    # -*- coding: utf-8 -*-
from __future__ import unicode_literals

from d         


        
5条回答
  •  被撕碎了的回忆
    2021-01-05 10:36

    I had the same error when I tried to get the auth user by writing: User = get_user_model() at the top of my models.py:

    from django.db import models
    from django.contrib.auth.models import AbstractUser
    from django.contrib.auth import get_user_model
    
    User = get_user_model()
    
    class User(AbstractUser):
        is_official = models.BooleanField('official status', default=False)
        is_distro = models.BooleanField('distro status', default=False)
        is_subscriber = models.BooleanField('subscriber status', default=False)
    

    I was able to resolve it by moving the User = get_user_model() below the User model definition, which makes sense as the get_user_model() getting called at the top of the User model definition meant it was referencing a model that doesn't exist yet. Here is the code layout that worked:

    from django.db import models
    from django.contrib.auth.models import AbstractUser
    from django.contrib.auth import get_user_model
    
    class User(AbstractUser):
        is_official = models.BooleanField('official status', default=False)
        is_distro = models.BooleanField('distro status', default=False)
        is_subscriber = models.BooleanField('subscriber status', default=False)
    
        def __str__(self):
            return self.username
    
    User = get_user_model()
    

    You may not have this exact layout but I guess the main point is To Not Reference A Model Before Its Definition In The Same File, This Is Why Import Statements Come At The Top.

提交回复
热议问题