身份验证用户模型不接受子应用程序

2024-10-06 14:22:21 发布

您现在位置:Python中文网/ 问答频道 /正文

在使用Django 1.7.1Python 3.4时,我的自定义用户模型有问题。 我在一个apps.users.AuthUser. 然后我有另一个申请(应用程序.pets)它将在Pet模型中使用AuthUser作为外键。见下文:

class Pet(models.Model):
    owner = models.ForeignKey(settings.AUTH_USER_MODEL, db_index=True, blank=False, null=False)

这是我的设想:

在我的INSTALLED_APPS中,我有:应用程序用户““

如果我设置AUTH_USER_MODEL="apps.users.AuthUser",则在我运行“runserver”时引发异常:

^{pr2}$

如果我设置AUTH_USER_MODEL=“用户.AuthUser“运行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

这是否意味着在AUTH_USR_MODEL设置中必须使用“app”_label.model_名称'而不是'apps.app_标签.型号?名称?有解决办法吗?在

编辑1: 我的项目结构如下:

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

Tags: apps用户py模型auth应用程序modelinit
1条回答
网友
1楼 · 发布于 2024-10-06 14:22:21

在你安装的应用程序中,你可以只使用你的应用程序名,假设它在同一个目录中,无需特别提及'我的应用程序用户'只要'myapp'就行了。在

使用这个:

AUTH_USER_MODEL = 'myapp.MyAuthUserModel'

不要使用这个:

^{pr2}$

希望这有帮助!在

相关问题 更多 >