Django: ImportError: cannot import name 'User'
Asked Answered
A

6

7

I'm new to django and I'm facing some difficulties in creating a user from the AbstractBaseUser model. Now I'm starting wondering if my user model is not being build in the right way. This is my User model

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

class User(AbstractBaseUser):
    '''
    Custom user class.
    '''
    username = models.CharField('username', max_length=10, unique=True, db_index=True)
    email = models.EmailField('email address', unique=True)
    joined = models.DateTimeField(auto_now_add=True)
    is_active = models.BooleanField(default=True)
    is_admin = models.BooleanField(default=False)

and user is only referenced in this other model

from django.db import models
from user_profile import User

class Tweet(models.Model):
    '''
    Tweet model
    '''
    user = models.ForeignKey(User)
    text = models.CharField(max_length=160)
    created_date = models.DateTimeField(auto_now_add=True)
    country = models.CharField(max_length=30)
    is_active = models.BooleanField(default=True)

then when I try to migrate my new model into db

Traceback (most recent call last):
  File "manage.py", line 22, in <module>
    execute_from_command_line(sys.argv)
  File "/home/myuser/django-book/lib/python3.5/site-packages/django/core/management/__init__.py", line 367, in execute_from_command_line
    utility.execute()
  File "/home/myuser/django-book/lib/python3.5/site-packages/django/core/management/__init__.py", line 341, in execute
    django.setup()
  File "/home/myuser/django-book/lib/python3.5/site-packages/django/__init__.py", line 27, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/home/myuser/django-book/lib/python3.5/site-packages/django/apps/registry.py", line 108, in populate
    app_config.import_models(all_models)
  File "/home/myuser/django-book/lib/python3.5/site-packages/django/apps/config.py", line 199, in import_models
    self.models_module = import_module(models_module_name)
  File "/home/myuser/django-book/lib/python3.5/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 986, in _gcd_import
  File "<frozen importlib._bootstrap>", line 969, in _find_and_load
  File "<frozen importlib._bootstrap>", line 958, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 673, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 665, in exec_module
  File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
  File "/home/myuser/django-book/mytweets/tweets/models.py", line 2, in <module>
    from user_profile import User
ImportError: cannot import name 'User'

I'm using django 1.10.1 on ubuntu 16.04 with python3.5 and mysql as database.

Aldosterone answered 18/9, 2016 at 15:56 Comment(3)
What's your directory structure? Are the two modules in different directories?Lapin
Did you try renaming 'User' model into something as 'UserProfile'. Try and use unique usernames for User models as django.contrib.auth.models also contains a model named as User.Tibbs
actually they were on different applications... But @Zulfugar 's answer solved my problem. It was a newbie mistakeAldosterone
H
8

You didn't import from right package.

Use:

from user_profile.models import User
Huesman answered 18/9, 2016 at 16:18 Comment(5)
when did it became mandatory to specify models after the application name? the tutorial that I'm following does not search dor the models in the submodule "models"Aldosterone
@Aldosterone your class is probably in models.py file and file is in application folder. That's why you should import from models.Huesman
I don't know why is this accepted. I get error ModuleNotFoundError: No module named 'user_profile' Ostentation
@Tessaracter you probably need a __init__.py file in your user_profile folderClinker
Big oof! I needed the ".models" portion of it... such a simple mistake!Kaduna
R
8

I solved this by importing

from django.contrib.auth.models import User
Rosanarosane answered 24/7, 2020 at 0:24 Comment(2)
You important the default User model, the question is about important his custom user modelSunday
This works for importing User, but why do they not state "django.contrib.auth.models" in the documentation?Tadpole
N
1

You can change it to

user = models.ForeignKey('<usermodel>.User', on_delete=models.CASCADE)

Nickienicklaus answered 11/2, 2023 at 9:4 Comment(0)
B
0

I solved this by importing

from django.contrib.auth.models import AbstractBaseUser

Check the right spell of the package

Bael answered 6/10, 2021 at 4:50 Comment(0)
C
0

I had a makemigrations problem while creating a new custom User model mid project.

My solution to this problem was to import CustomNameUser from users.models app.

Might help those with similar questions to yours but with a similar context.

CustomNameUser being the class name of the custom User model that you extended AbstractUser or AbstractBaseUser

Sample code: users/models.py

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

class CustomUser(AbstractUser):
    class Meta:
        db_table = 'auth_user'

app/models.py

...
from users.models import CustomUser
...

class ClassName():
  ...
  user = models.ForeignKey(CustomUser)
  ...

  
Conveyor answered 1/11, 2022 at 17:39 Comment(0)
R
-1

Make sure you import User and not user

'' from django.contrib.auth.models import User

Ripplet answered 27/8, 2021 at 10:22 Comment(2)
This is the same solution as in this other answer.Carpentaria
This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker.Cabalism

© 2022 - 2024 — McMap. All rights reserved.