django.core.exceptions.ImproperlyConfigured: AUTH_USER_MODEL refers to model 'auth.User' that has not been installed
Asked Answered
H

21

20

After migrating my django and userena packages like below

Django 1.8 to Django 1.9.7

django-userena 1.4.1 to django-userena==2.0.1

After running the project , I am getting below this error

Unhandled exception in thread started by <function wrapper at 0xb689641c>
Traceback (most recent call last):
File "/home/Documents/environments/venv/local/lib/python2.7/site-packages/django/utils/autoreload.py", line 226, in wrapper
fn(*args, **kwargs)
File "/home/Documents/environments/venv/local/lib/python2.7/site-packages/django/core/management/commands/runserver.py", line 116, in inner_run
self.check(display_num_errors=True)
File "/home/Documents/environments/venv/local/lib/python2.7/site-packages/django/core/management/base.py", line 426, in check
include_deployment_checks=include_deployment_checks,
File "/home/Documents/environments/venv/local/lib/python2.7/site-packages/django/core/checks/registry.py", line 75, in run_checks
new_errors = check(app_configs=app_configs)
File "/home/Documents/environments/venv/local/lib/python2.7/site-packages/django/core/checks/urls.py", line 13, in check_url_config
return check_resolver(resolver)
File "/home/Documents/environments/venv/local/lib/python2.7/site-packages/django/core/checks/urls.py", line 23, in check_resolver
for pattern in resolver.url_patterns:
File "/home/Documents/environments/venv/local/lib/python2.7/site-packages/django/utils/functional.py", line 33, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/home/Documents/environments/venv/local/lib/python2.7/site-packages/django/core/urlresolvers.py", line 417, in url_patterns
patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "/home/Documents/environments/venv/local/lib/python2.7/site-packages/django/utils/functional.py", line 33, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/home/Documents/environments/venv/local/lib/python2.7/site-packages/django/core/urlresolvers.py", line 410, in urlconf_module
return import_module(self.urlconf_name)
File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
File "/home/Documents/swamy/sample_project/july/5/sample11/sampleapp/urls.py", line 28, in <module>
(r'^grappelli/', include('grappelli.urls')),
File "/home/Documents/environments/venv/local/lib/python2.7/site-packages/django/conf/urls/__init__.py", line 52, in include
urlconf_module = import_module(urlconf_module)
File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
File "/home/Documents/environments/venv/local/lib/python2.7/site-packages/grappelli/urls.py", line 8, in <module>
from .views.switch import switch_user
File "/home/Documents/environments/venv/local/lib/python2.7/site-packages/grappelli/views/switch.py", line 18, in <module>
User = get_user_model()
File "/home/Documents/environments/venv/local/lib/python2.7/site-packages/django/contrib/auth/__init__.py", line 155, in get_user_model
"AUTH_USER_MODEL refers to model '%s' that has not been installed" % settings.AUTH_USER_MODEL

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

Here are INSTALLED_APPS in my settings file,

'grappelli.dashboard',
'grappelli',
'filebrowser',     
'django.contrib.admindocs',
'django.contrib.admin',
'django.contrib.auth',    
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.sites',
'django.contrib.staticfiles',  
'django.contrib.redirects',
'django.contrib.sitemaps',
'haystack',  
'memcache_status',   
'stheme',    
'home',
'customers',
'orders',
#'legacy',
'products',
'bloglets',
'utils',
'catax',
'sqls',
'quotes',    
#'django_stylus',
#'djgrid',
#'obdjects',
'quickpages', 
'loginas',
#'pyjade',
'django_countries',  
'debug_toolbar',
'djide',
#'dbtemplates',  
#'aloha',  # out temporarily, migrate to alternate https://github.com/ntucker/django-aloha-edit - JJW
'coffeescript',
'django_wysiwyg',
#'django_bfm',
'userena',
'guardian', 
#'apps',  
#'filer',
'easy_thumbnails', 
'taggit',
#'taggit_templatetags',
# 'social_auth',    
'social.apps.django_app.default', 
#'socialregistration',
#'socialregistration.contrib.linkedin',
'email_extras',
#'csvimport', 
'csvimport.app.CSVImportConf',  
'django_extensions',
'webshell',
'easy_select2',  
#'plata',
#'plata.contact', 
#'plata.discount',
#'plata.payment',
#'plata.shop',
'lastmodule',

I guess there are some changes in python apps.But I can't find the reason... Does anyone help to fix this issue ?

Thanks In Advance !

Harriettharrietta answered 5/7, 2016 at 12:19 Comment(2)
Try ./manage.py migrate auth to migrate just the auth package first then ./manage.py migrate?Lubbi
for me it was syntax error in installed_apps list, one comma missingDennisedennison
G
7

This problems generally resides between 2 reasons.

  1. When the dependencies order in installed apps are reversed.
  2. When you haven't mentioned the dependency in the installed apps at all.

Here in this case grappelli seems to raise the issue telling auth.User not found. It means it is not able to find any package auth. If you are using the default user model remove that AUTH_USER_MODEL setting from the config or if you are using any custom user model in package 'auth' list it in installed apps.

Guesthouse answered 5/4, 2017 at 9:45 Comment(1)
Great. worked for me, problem was ordering of the installed apps.Yt
K
6

I run into the op issue as soon as I decided to migrate from a single models.py file to a models folder containing a user.py file to define the custom User model.

In that case, as explained here, the trick is to ensure to import your model info the __init__.py file of your models folder.

For example, if your directory structure is:

profiles
    models
        __init__.py
        user.py

In the __init__.py file, add from .user import User

Kehoe answered 13/9, 2022 at 9:25 Comment(0)
P
2

A complete traceback would help to diagnose it better. Prima facie, it seems to me as a dependency issue caused due to migration. Check what Django docs have to say about this -

Due to limitations of Django’s dynamic dependency feature for swappable models, you must ensure that the model referenced by AUTH_USER_MODEL is created in the first migration of its app (usually called 0001_initial); otherwise, you will have dependency issues.

Here's the link - https://docs.djangoproject.com/en/1.9/topics/auth/customizing/

Psychic answered 5/7, 2016 at 12:50 Comment(1)
Thank you , I will try once after that I will let you knowHarriettharrietta
M
2

if your application is not called auth you have to replace it:
AUTH_USER_MODEL='your_app_name.User'

Marnie answered 10/6, 2020 at 22:53 Comment(2)
By just replacing the model from UserProfile to User fixed my issueFlit
Its actually app_label not app_name, they are two different things.Fecteau
A
1

Once I had the same issue in v-2.2 then I realized that the spelling of "register" in the admin.py file was wrong.

Andri answered 16/6, 2020 at 14:2 Comment(0)
T
1

This is becuase you have an app 'auth' in which you have defined your user model. and You have not mentioned app name in INSTALLED_APPS dictionary.

Try Adding your app name in INSTALLED_APPS and check.

Thermoelectrometer answered 31/7, 2020 at 8:20 Comment(0)
J
1

Make sure you write your code of custom user in models.py file.

Judicator answered 5/5, 2022 at 13:52 Comment(1)
Yes! In the case you still want to use a custom architecture, please see my answer.Kehoe
S
0

This error may also appear due to a missing import of a model in an admin.py (Django version 2.2).

Scotticism answered 30/1, 2020 at 19:21 Comment(0)
P
0

Try make the migrations first; python manage.py makemigrations. This might detect if you have any problems with your imports which you can fix then run the server to verify

Predation answered 5/2, 2020 at 6:34 Comment(1)
This answer is irrelevant to the question. Running any python manage.py will give the same error without any additional context.Pronucleus
P
0

I doubt this is the typical answer, but I hit this problem when I using reverse and should have been using reverse_lazy. Changing to reverse_lazy fixed it for me.

Pomiculture answered 21/4, 2020 at 17:52 Comment(0)
H
0

This error appeared when I had an improperly configured model and added it to the admin backend.

Wrong:
   class VolunteerExperience:
 ...

Correct:
   class VolunteerExperience(models.Model):
 ...

I hope this helps someone who gets misdirected by the exception message "django.core.exceptions.ImproperlyConfigured: AUTH_USER_MODEL refers to model 'auth.User' that has not been installed"

Now I know that this message most likely indicates an incorrectly configured model.

Harpole answered 30/5, 2020 at 17:32 Comment(0)
P
0

In addition to all the other answers, I ran into this issue when I upgraded Django. I had an import that was deprecated from one release to another. Make sure to check the release notes/version histories before upgrading any package.

https://docs.djangoproject.com/en/dev/releases/

Pronucleus answered 3/2, 2021 at 16:41 Comment(0)
H
0

if anyone get this error again, maybe this will help

I hardly struggled to debug this error the whole day.

The problem is that I wrote the model in my main app and not in the authentication app.

Harryharsh answered 11/11, 2022 at 12:18 Comment(0)
A
0

I spent a lot of time in debugging this but I don't know how this problem was occurring but this just got solved when I change my class name from:

class User(AbstractUser):
#to
class CustomUser(AbstractUser):
#and in settings.py:
AUTH_USER_MODEL = 'api.CustomUser'
Ajar answered 23/2, 2023 at 12:19 Comment(0)
M
0

I had the same issue and solved by:

Commenting out USER GROUPS that I had created in the models.py; forexample.

TA,created=Group.objects.get_or_create(name='teachers')

Final word:

  1. Comment your defined user groups forexample: #TA,created=Group.objects.get_or_create(name='teachers')
  2. Run python makemigrations your_project_name
  3. python migrate your_project_name
  4. Then, uncomment your defined your groups: TA,created=Group.objects.get_or_create(name='teachers')

And you are good to go.

Michaelis answered 27/9, 2023 at 15:10 Comment(0)
H
0

In my case, I had to update the admin.py file and include my user model. For example, I have a folder called model inside my app, which has a folder login and a usuario.py file with a Usuario class.

Then, in the app folder, I updated the admin.py file to include that model.

from django.contrib import admin

# Register your models here.
from .model.login.usuario import Usuario
Hanzelin answered 28/9, 2023 at 16:40 Comment(0)
R
0

For me, it turned out to be the placement of an import statement.

I needed to import model Notes into my users/models.py from a separate Django project where I couldn't change anything.

Model Notes had a fk-field to User via django.contrib.auth.get_user_model(). I needed to inherit this model in the same models.py file as my custom User model.

Moving the import statement after the User model fixed the error:

from django.contrib.auth.models import AbstractUser


class UserProfile(AbstractUser):
    ...


from notes_project.notes.models import Note


class SpecialNotes(Notes):
    ...
Rapper answered 8/11, 2023 at 14:57 Comment(0)
J
0

I face same issue.

But my problem is, i forgot the add app_name in settings.py

Jacobian answered 25/12, 2023 at 12:16 Comment(0)
N
0

myapp/ authentication/ users.py models.py

my models.py contains from .authentication.users import *

that fixed my error

Northernmost answered 19/1 at 12:21 Comment(0)
B
0

Check what's imported in the models.py file that defines your custom user model. In my case, I faced this issue when implementing a custom user model and it was caused by the below import in models.py.

#project/accounts/models.py
from django.contrib.auth.backends import BaseBackend

Check the traceback of the exception carefully as the offending import will be in this trace. Seems like the below import is causing the issue here as it calls get_user_model().

from .views.switch import switch_user
Boarfish answered 1/2 at 20:41 Comment(0)
M
0

BaseBackend needs to be imported after importing the package from django.contrib.auth import get_user_model after that needs the custom User model needs to be implemented importing BaseBackend will work only work if imported after Model get declared. BaseBackend will initially look for the get_user if we import before the declaration this error causes.

from django.contrib.auth import get_user_model
class User(AbstractUser, models.Model):
    username = models.CharField(max_length=20, unique=True)
    hashed_mobilenumber = models.CharField(max_length=100, unique=True)
    ...

from django.contrib.auth.backends import BaseBackend
class MobileNumberAuthBackend(BaseBackend):
    def authenticate(self, request: HttpRequest, username: str = None, password: str = None, **kwargs: Any) -> AbstractBaseUser:
    pass
Margarita answered 4/6 at 17:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.