2016-07-28 32 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

相關問題