我正在开发一个 Django 1.5 项目,并且我有一个自定义用户模型(我们称之为CustomUser
)。另一个应用程序 (SomeApp) 需要引用此自定义用户模型。出于 ForeignKey 等的目的,Django 文档说要使用
User = settings.AUTH_USER_MODEL
但是,SomeApp.models 中的某些函数需要访问以前称为User.objects
. 但是 User 现在是一个字符串而不是一个类,所以User.objects
失败了。替代方案是
from django.contrib.auth import get_user_model
User = get_user_model()
这适用于其他模块,但是当我在 SomeApp 的 models.py 中使用它时,Django 会引发错误:
ImproperlyConfigured("AUTH_USER_MODEL 引用了尚未安装的模型 '%s'" % 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
编辑 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',
)