Django: ImportError: cannot import name 'User'

Question:

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.

Asked By: WisdomPill

||

Answers:

You didn’t import from right package.

Use:

from user_profile.models import User

I solved this by importing

from django.contrib.auth.models import User
Answered By: ChillieCode

Make sure you import User and not user

from django.contrib.auth.models import User

Answered By: Joseph Mwangi

I solved this by importing

from django.contrib.auth.models import AbstractBaseUser

Check the right spell of the package

Answered By: kumaresan-techie

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)
  ...

  
Answered By: hmfzer

You can change it to

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

Answered By: akash joshi