2016-07-28 40 views
1

的Django 1.9.7 我使用pyenv的virtualenv autoenvLookupError:应用程序“用户”不具有“用户”模式

我想要扩展用户模型于是,我决定使用AbstractUser

(AbstractUser的class META抽象=真,所以我不能让表,但继承类可以使右表??)

反正

(WEF是项目名称) 我让应用wef/users/models/__init__.py

from .user import User 

wef/users/models/user.py

from django.contrib.auth.models import AbstractUser 

from django.db import models 


class User(AbstractUser): 

    phonenumber = models.CharField(
      max_length = 11, 
      blank = True, 
      null = True 
      ) 

,我加入settings.py users应用

INSTALLED_APPS = [ 
    [...] 
    'users', 
] 

AUTH_USER_MODEL = 'users.User' 

所以,我觉得当我makemigrations,迁移

Django会做模型关于用户的表...

python wef/manage.py makemigrations users

它显示错误

Traceback (most recent call last): 
    File "/Users/hanminsoo/.pyenv/versions/study_alone/lib/python3.5/site-packages/django/apps/config.py", line 163, in get_model 
return self.models[model_name.lower()] 
KeyError: 'user' 

During handling of the above exception, another exception occurred: 

Traceback (most recent call last): 
    File "manage.py", line 10, in <module> 
execute_from_command_line(sys.argv) 
    File "/Users/hanminsoo/.pyenv/versions/study_alone/lib/python3.5/site-packages/django/core/management/__init__.py", line 353, in execute_from_command_line 
utility.execute() 
    File "/Users/hanminsoo/.pyenv/versions/study_alone/lib/python3.5/site-packages/django/core/management/__init__.py", line 345, in execute 
self.fetch_command(subcommand).run_from_argv(self.argv) 
    File "/Users/hanminsoo/.pyenv/versions/study_alone/lib/python3.5/site-packages/django/core/management/base.py", line 348, in run_from_argv 
self.execute(*args, **cmd_options) 
    File "/Users/hanminsoo/.pyenv/versions/study_alone/lib/python3.5/site-packages/django/core/management/base.py", line 398, in execute 
self.check() 
    File "/Users/hanminsoo/.pyenv/versions/study_alone/lib/python3.5/site-packages/django/core/management/base.py", line 426, in check 
include_deployment_checks=include_deployment_checks, 
    File "/Users/hanminsoo/.pyenv/versions/study_alone/lib/python3.5/site-packages/django/core/checks/registry.py", line 75, in run_checks 
new_errors = check(app_configs=app_configs) 
    File "/Users/hanminsoo/.pyenv/versions/study_alone/lib/python3.5/site-packages/django/contrib/auth/checks.py", line 12, in check_user_model 
cls = apps.get_model(settings.AUTH_USER_MODEL) 
    File "/Users/hanminsoo/.pyenv/versions/study_alone/lib/python3.5/site-packages/django/apps/registry.py", line 197, in get_model 
return self.get_app_config(app_label).get_model(model_name.lower()) 
    File "/Users/hanminsoo/.pyenv/versions/study_alone/lib/python3.5/site-packages/django/apps/config.py", line 166, in get_model 
"App '%s' doesn't have a '%s' model." % (self.label, model_name)) 
LookupError: App 'users' doesn't have a 'user' model. 

我不明白为什么Django中无法找到users.User模型

,当我改变`AUTH_USER_MODEL = UserAAA”

它显示错误(大写字母更改为小写)

LookupError: App 'users' doesn't have a 'useraaa' model.

我无法找到我的问题 请人帮助我..ㅠ_ㅠ

+2

是你的包真“模式”,而不是“模型”前有from .user import User? –

+0

哦对不起编辑我的错误,但它有相同的错误 –

+0

请有人帮助我我真的需要任何人的帮助 –

回答

3

我想你已经创建了数据库模式。从Django的文档:

Changing AUTH_USER_MODEL has a big effect on your database structure. It changes the tables that are available, and it will affect the construction of foreign keys and many-to-many relationships. If you intend to set AUTH_USER_MODEL, you should set it before creating any migrations or running manage.py migrate for the first time.

Changing this setting after you have tables created is not supported by makemigrations and will result in you having to manually fix your schema, port your data from the old user table, and possibly manually reapply some migrations.

+0

谢谢你@Gupta我会尝试删除db.sqlite3和'manage.py reset_db' –

+1

你必须解决我的问题。谢谢!!!谢谢!!非常!! –

+1

我有同样的问题,但我一直使用这个自定义用户模型。我刚刚从'models.py'移动到'models/__ init __。py'在那种情况下可能会出现什么问题? –

1

我有更多的选项来解决这个问题。

  1. 如果你真的想从该contrib.auth

迁移将db_table = 'auth_user'在模型中的元使用现有的表并没有攻破对方的关系。进行没有任何自定义字段的迁移,然后进行模型更改和其他迁移。

  1. 我将models.py移动到models/init。PY

在这种情况下,我需要导入任何其他模型称为get_user_model()

+0

感谢第一点,亲爱的互联网陌生人。 – Chase

相关问题