2016-07-28 4 views
1

ジャンゴ1.9.7 私はpyenv virtualenvのautoenvにLookupErrorが:アプリの「ユーザー」「ユーザー」のモデルを持っていない

を使用しています私はユーザモデルを拡張したいが、私はAbstractUser

を使用することを決定します

(AbstractUserのclass META真=抽象的なので、私はテーブルを作ることはできませんが、継承クラスは??権利、テーブルを作ることができる)とにかく

私はアプリを作る (WEFはプロジェクト名です) 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 
      ) 

と私は

from .user import User 

だから、settings.py

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

AUTH_USER_MODEL = 'users.User' 

usersアプリを追加し、私は私makemigrationsは、

Djangoがモデルを行います移行する場合を考えますユーザーについてのテーブル...

python wef/manage.py makemigrations users

それは示して、私はDjangoがusers.Userモデル

見つけることができない理由を理解することはできません

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. 

エラーと私は変更 `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/にモデルを移動しました。その場合、PY

私はget_user_model()

+0

ポイント1の親愛なるインターネット見知らぬ人に感謝します。 – Chase

関連する問題