我正在開發一個Django 1.5項目,並且我有一個自定義用戶模型(我們稱之爲CustomUser
)。另一個應用程序(SomeApp)需要引用此自定義用戶模型。對於ForeignKey的和這樣的目的,Django文檔說,使用Django 1.5:訪問models.py中的自定義用戶模型字段
User = settings.AUTH_USER_MODEL
然而,在SomeApp.models某些功能需要訪問什麼將以前被稱爲User.objects
。但用戶現在是一個字符串,而不是一個類,所以User.objects
失敗。另一種方法是
from django.contrib.auth import get_user_model
User = get_user_model()
在其他模塊,這些模塊的工作原理,但是當我使用這SomeApp的models.py,Django的引發錯誤:
ImproperlyConfigured("AUTH_USER_MODEL refers to model '%s' that has not been installed" % settings.AUTH_USER_MODEL)
任何想法?
編輯1 - 回溯:
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "...\django-badger\badger\__init__.py", line 7, in <module>
from badger.models import Badge, Award, Progress
File "...\django-badger\badger\models.py", line 26, in <module>
User = get_user_model()
File "...\lib\site-packages\django\contrib\auth\__init__.py", line 127, in get_user_model
raise ImproperlyConfigured("AUTH_USER_MODEL refers to model '%s' that has not been installed" % settings.AUTH_USER_MODEL)
ImproperlyConfigured: AUTH_USER_MODEL refers to model 'MyApp.AuthUser' that has not been installed
EDIT 2 - INSTALLED_APPS設置:
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.admin',
'django.contrib.admindocs',
'south',
'MyApp', # this is where my user model is defined
'SomeApp', # I try to use get_user_model() in this app's models.py; doesn't work.
'social_auth',
)
你可以發佈完整的追溯? – jpic
你是如何在你的設置中定義'AUTH_USER_MODEL'的?用戶是哪個應用程序,它的類名是什麼? – matino
@matino - 是的,它是在設置中定義的。 get_user_model()在除models.py之外的所有其他文件中工作正常。我的自定義用戶模型AuthUser在MyApp中定義(在本例中) – askvictor