我是django的新手,在从AbstractBaseUser模型创建用户时面临一些困难。现在我开始想知道我的用户模型是不是以正确的方式构建的。这是我的用户模型Django:ImportError:无法导入名称'User'
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)
,当我尝试我的新模型迁移到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'
我使用的用户只在这个其他模型中引用
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)
然后django 1.10.1在ubuntu 16.04上用python3.5和mysql作为数据库。
什么是你的目录结构?这两个模块在不同的目录中吗? –
您是否尝试将'User'模型重命名为'UserProfile'。尝试使用用户模型的唯一用户名,因为'django.contrib.auth.models'还包含一个名为'User'的模型。 –
实际上他们在不同的应用程序......但@Zulfugar的答案解决了我的问题。这是一个新手错误 – WisdomPill