我正在开发一个Django 1.5项目,其中包含自定义用户模型(我们称之为CustomUser
)。另一个应用程序(SomeApp)需要引用这个自定义用户模型。根据Django文档的说明,在ForeignKey等情况下,应使用以下代码:
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 = (
'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',
)
AUTH_USER_MODEL
的?用户位于哪个应用程序中,其类名是什么? - matinoINSTALLED_APPS
设置吗? - jpicget_user_model()
而不是直接引用类名? - matino