AUTH_USER_MODEL不接受子申请 [英] AUTH_USER_MODEL does not accept sub application
问题描述
在使用 Django 1.7.1
和 Python 3.4
时,我的自定义用户模型存在问题.我在apps.users.AuthUser中声明了自定义用户模型.然后,我有了另一个应用程序(apps.pets),该应用程序将AuthUser用作Pet模型中的ForeignKey.见下文:
I am having problems with my custom user model while using Django 1.7.1
and Python 3.4
.
I have declared a Custom user model in an apps.users.AuthUser. I then have another application (apps.pets) that will use the AuthUser as a ForeignKey in a Pet model. See below:
class Pet(models.Model):
owner = models.ForeignKey(settings.AUTH_USER_MODEL, db_index=True, blank=False, null=False)
这是我的情况:
在我的 INSTALLED_APPS
中,我有:"apps.users"
In my INSTALLED_APPS
I have: "apps.users"
如果我设置 AUTH_USER_MODEL ="apps.users.AuthUser"
,则在运行"runserver"时会引发异常:
If I set AUTH_USER_MODEL="apps.users.AuthUser"
the exception is raised when I run 'runserver':
File "/home/frank/.virtualenvs/myproj/lib/python3.4/site-packages/django/contrib/auth/checks.py", line 12, in check_user_model
cls = apps.get_model(settings.AUTH_USER_MODEL)
File "/home/frank/.virtualenvs/myproj/lib/python3.4/site-packages/django/apps/registry.py", line 201, in get_model
app_label, model_name = app_label.split('.')
ValueError: too many values to unpack (expected 2)
如果我设置AUTH_USER_MODEL ="users.AuthUser",则在运行迁移时会引发异常:
If I set AUTH_USER_MODEL="users.AuthUser" the exception is raised when I run migrate:
File "/home/frank/.virtualenvs/myproj/lib/python3.4/site-packages/django/db/migrations/state.py", line 89, in render
model=lookup_model,
ValueError: Lookup failed for model referenced by field pets.Pet.owner: users.AuthUser
我在这里报告了类似的问题: https://code.djangoproject.com/ticket/19845
Similar issue reported here I guess: https://code.djangoproject.com/ticket/19845
这是否意味着在 AUTH_USR_MODEL
设置中,我必须使用"app_label.model_name"而不是"apps.app_label.model_name"?有解决方法吗?
Does this mean that in AUTH_USR_MODEL
setting I have to use 'app_label.model_name' instead of 'apps.app_label.model_name.'? Is there a workaround for this?
我的项目结构如下:
SITE_ROOT
|-- PROJECT_ROOT/
| |-- apps/
| | |-- app1
| | |-- app2
| |-- etc/
| |-- libs/
| |-- media/
| |-- requirements/
| | |-- __init__.py
| | |-- common.py
| | |-- development.py
| | |-- production.py
| | `-- staging.py
| |-- settings/
| | |-- __init__.py
| | |-- common.py
| | |-- development.py
| | |-- production.py
| | `-- staging.py
| |-- templates/
| |-- README
| |-- __init__.py
| |-- .gitignore
| `-- manage.py
推荐答案
在已安装的应用程序中,您可以仅假设应用程序名称位于相同目录中,而无需特别提及"myapp.users",而只需"myapp"即可做.
In your installed apps you can have just your application name assuming its in the same directory, no need to specifically mention 'myapp.users' just 'myapp' will do.
使用此:
AUTH_USER_MODEL = 'myapp.MyAuthUserModel'
请勿使用此功能:
AUTH_USER_MODEL = 'myapp.models.MyAuthUserModel'
希望这会有所帮助!
这篇关于AUTH_USER_MODEL不接受子申请的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!