Django:导入错误:无法导入名称'User'

rm5edbpk  于 2023-02-14  发布在  Go
关注(0)|答案(6)|浏览(216)

我是django的新手,在从AbstractBaseUser模型创建用户时遇到了一些困难。现在我开始怀疑我的用户模型是否没有以正确的方式构建。这是我的用户模型

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)

用户仅在此其他模型中引用

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)

当我尝试将新模型迁移到数据库时

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'

我在ubuntu16.04上使用django1.10.1,用python3.5和mysql作为数据库。

svdrlsy4

svdrlsy41#

您没有从正确的包导入。
use:

from user_profile.models import User
uxh89sit

uxh89sit2#

我通过导入

from django.contrib.auth.models import User
mmvthczy

mmvthczy3#

我通过导入

from django.contrib.auth.models import AbstractBaseUser

检查包裹的正确拼写

gfttwv5a

gfttwv5a4#

我有一个makemigrations的问题,而创建一个新的自定义用户模型中期项目。
我对此问题的解决方案是从users.models应用程序导入CustomNameUser。
可能会帮助那些有类似问题的人,但有类似的背景。
CustomNameUser是您扩展的AbstractUser或AbstractBaseUser的自定义用户模型的类名
样本代码:用户/示例网站models.py

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

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

应用程序/示例网站models.py

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

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

fumotvh35#

您可以将其更改为
user = models.ForeignKey('<usermodel>.User', on_delete=models.CASCADE)

sxissh06

sxissh066#

确保导入用户而不是用户
“”from django.contrib.auth.models import User

相关问题