2

众所周知,Django-Cookiecutter 对设置文件有不同的设置。来自 django.conf的常规导入设置在这里不起作用。

我想引用设置目录下base.py 文件中定义的自定义用户模型。有任何想法吗?

下面是我的项目布局:

repository/
    config/
        settings/
            __init__.py
            base.py # where the auth_user_model variable is defined
            local.py
            production.py
            test.py
    app_dir/
        users/
            __init__.py
            models.py # where the custom user model is stored

我还尝试直接从 users/models.py 导入自定义用户模型,如下所示:

from users.models import User

但出现以下错误:

RuntimeError: Model class users.models.User doesn't declare an 
explicit app_label and isn't in an application in INSTALLED_APPS.
4

2 回答 2

3

尝试了以下方法,到目前为止它似乎有效:

from config.settings.base import AUTH_USER_MODEL
于 2017-08-18T07:08:45.697 回答
0
from django.contrib.auth import get_user_model

它将返回您在 base.py 中定义的 django 用户类

编辑:

from django.conf.settings import AUTH_USER_MODEL
于 2017-08-18T07:37:18.730 回答