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

标签 python django django-models model django-users

我是 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)

然后当我尝试将我的新模型迁移到 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'

我在 ubuntu 16.04 上使用 django 1.10.1,使用 python3.5 和 mysql 作为数据库。

最佳答案

您没有从正确的包中导入。

用:

from user_profile.models import User

关于python - Django:导入错误:无法导入名称 'User',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39559635/

相关文章:

python - 在文本文件中搜索相似字母并返回有效单词列表

java - Google Cloud Storage 通过 App Engine 定价

python - 正确的xpath是什么?

Django 外键 : settings. AUTH_USER_MODEL 一直为 form.save 提供空值

python - Django ORM 中的 SumIf(条件求和)

python - 如何在 Django Python 中从子类访问父类字段

终端中的 Python 编译

django - 在 nginx 上部署 Django

python - 是否可以使用 DJango 运行 ubuntu 终端命令

Django:使 CustomUser 出现在 Auth 下的管理员中