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

前端 未结 5 1842
渐次进展
渐次进展 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:26

    For me it worked when I tried shifting the position of the app in INSTALLED_APPS.

    0 讨论(0)
  • 2021-01-05 10:28

    it's an import issue: try changing the order of the imports: try: from django.contrib.auth.models import AbstractUser then: from django.db import models

    0 讨论(0)
  • 2021-01-05 10:36

    To better understand the issue, I'll illustrate how I solved this problem. For me moving one of the import statement (which internally used the User model) from the top of the file and placing it after the User model is defined fixed the issue

    from django.contrib.auth.models import AbstractUser, BaseUserManager
    from django.db import models
    from django.db.models.signals import post_save
    from rest_framework.authtoken.models import Token
    from .utils import generate_token, send_email, generate_id
    
    
    
    class User(AbstractUser):
        """User model."""
    
        username   = None
        email      = models.EmailField(_('email address'), unique=True)
    
        USERNAME_FIELD = 'email'
        REQUIRED_FIELDS = []
    
        objects = UserManager()
    
    #### Moved the import statement here so that the issue is solved.
    #### This function internally used User model which is only defined right above this.
    from meter.tasks import apigeePipeline
    
    def post_save_user_receiver(sender, instance, created, *args, **kwargs):
        if created:
            print("***********USer created**********")
            token = Token.objects.create(user=instance)
            send_email(instance.email, token.key)
            apigeePipeline(instance)
    
    
    post_save.connect(post_save_user_receiver, sender=User)
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2021-01-05 10:38

    I suspect the problem is one of dependencies. You are importing UserCreationForm at the top of your accounts.models file, where it in turn tries to get the user model - but the rest of that models file has not yet been processed, so User is not defined.

    You can easily solve this by following recommended practice and moving the form import and definition into a separate forms.py file.

    0 讨论(0)
提交回复
热议问题