2

As we know, Django-Cookiecutter has a different setup for settings files. The regular from django.conf import settings doesn't work here.

I want to reference to the custom user model defined in the base.py file in the settings directory. Any ideas?

Below is my project layout:

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

I also tried to import the custom user model directly from users/models.py as below:

from users.models import User

But got the following error:

RuntimeError: Model class users.models.User doesn't declare an 
explicit app_label and isn't in an application in INSTALLED_APPS.
Arpit Solanki
  • 9,567
  • 3
  • 41
  • 57
Zilong Li
  • 889
  • 10
  • 23

2 Answers2

3

Tried the following, and it seems to work so far:

from config.settings.base import AUTH_USER_MODEL
Zilong Li
  • 889
  • 10
  • 23
  • A related change I had to make in my tests was to use absolute import paths instead of relative paths. e.g from ..views import (...) gave me the same runtime error. – Brad Rhoads Jun 26 '18 at 21:18
0
from django.contrib.auth import get_user_model

It'll return django user class that you defined in base.py

Edit:

from django.conf.settings import AUTH_USER_MODEL
Neeraj Kumar
  • 3,851
  • 2
  • 19
  • 41
  • Thanks for the answer. I'm using Django 1.10. I think there's still a difference b/w get_user_model and AUTH_USER_MODEL, somehow? I'm trying to reference the current User model in another model, so maybe using AUTH_USER_MODEL is better. – Zilong Li Aug 19 '17 at 03:34
  • I think you should read django documentation. I added that code is standart code of django. django.conf.settings have all vars value of settings dir. – Neeraj Kumar Aug 20 '17 at 05:52