Opened 2 years ago

Closed 2 years ago

Last modified 2 years ago

#19845 closed Bug (invalid)

AUTH_USER_MODEL not accept sub application.

Reported by: eltonplima@… Owned by: nobody
Component: Uncategorized Version: 1.5-rc-1
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

In settings.py to use a custom user AUTH_USER_MODEL i have to set the format app_label.model_name.

But my custom User model is within the following structure cadastro.pessoa.Usuario and django is not accepting.

Attachments (2)

models.py (12.1 KB) - added by eltonplima@… 2 years ago.
The models.py
models.2.py (10.3 KB) - added by eltonplima@… 2 years ago.
I've sent the wrong file.

Download all attachments as: .zip

Change History (10)

comment:1 Changed 2 years ago by aaugustin

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Resolution set to invalid
  • Status changed from new to closed

The app label is the last part of the dotted path to the app. It's the usual convention in Django.

comment:2 Changed 2 years ago by anonymous

In my INSTALLED_APPS i have: "cadastro.pessoa"

If i set AUTH_USER_MODEL="cadastro.pessoa.Usuario" the exception is raised:
ImproperlyConfigured: AUTH_USER_MODEL must be of the form 'app_label.model_name'

If i set AUTH_USER_MODEL="pessoa.Usuario" the exception is raised:
ImproperlyConfigured: AUTH_USER_MODEL refers to model 'pessoa.Usuario' that has not been installed

comment:3 Changed 2 years ago by aaugustin

I suspect something's wrong with your model definition. Can you upload cadastro/pessoa/models.py?

Changed 2 years ago by eltonplima@…

The models.py

Changed 2 years ago by eltonplima@…

I've sent the wrong file.

comment:4 follow-up: Changed 2 years ago by matt@…

I was receiving the following validation error when running django-admin.py validate:

ImproperlyConfigured: AUTH_USER_MODEL refers to model 'account.CustomUser' that has not been installed

The culprit was an ImportError in my account.models file.

comment:5 in reply to: ↑ 4 ; follow-up: Changed 2 years ago by eltonplima@…

Replying to matt@…:

I was receiving the following validation error when running django-admin.py validate:

ImproperlyConfigured: AUTH_USER_MODEL refers to model 'account.CustomUser' that has not been installed

The culprit was an ImportError in my account.models file.

Excuse me, but beyond a ImportError, does that mean?

comment:6 in reply to: ↑ 5 Changed 2 years ago by matt@…

Replying to eltonplima@…:

I have now discovered the true source of my problem. My models.py file was importing some custom functions from a separate file. This file was also calling the django.contrib.auth.get_user_model function. If you call that function before your custom user model has been installed, it will raise an ImproperlyConfigured exception.

The two error messages mentioned in comment#2 are both included as part of the django.contrib.auth.get_user_model function. You might want to check through your code, ensuring that your custom user models.py file is not importing anything that calls that function.

comment:7 Changed 2 years ago by esoergel@…

  • Resolution invalid deleted
  • Status changed from closed to new
  • Type changed from Uncategorized to Bug

I'm running into this same issue, but I believe it is a bug in the code. Here is the relevant line:
app_label, model_name = settings.AUTH_USER_MODEL.split('.')
This is a problem, if for example, your AUTH_USER_MODEL looks like this:
AUTH_USER_MODEL = "apps.accounts.UserAccount"
split() will return a three-element list and raise an "ValueError: too many values to unpack", triggering the exception.

I've copied the function below and linked to the line on github.

https://github.com/django/django/blob/master/django/contrib/auth/__init__.py#L117

def get_user_model():
    """
    Returns the User model that is active in this project.
    """
    from django.db.models import get_model

    try:
        app_label, model_name = settings.AUTH_USER_MODEL.split('.')
    except ValueError:
        raise ImproperlyConfigured("AUTH_USER_MODEL must be of the form 'app_label.model_name'")
    user_model = get_model(app_label, model_name)
    if user_model is None:
        raise ImproperlyConfigured("AUTH_USER_MODEL refers to model '%s' that has not been installed" % settings.AUTH_USER_MODEL)
    return user_model

comment:8 Changed 2 years ago by russellm

  • Resolution set to invalid
  • Status changed from new to closed

No, this is *not* an error in Django; it's an error in your code.

AUTH_USER_MODEL *is not* a dotted module include path. It's an "app name.model name" definition, the same as you would get in a fixture file.

AUTH_USER_MODEL = "apps.accounts.UserAccount" is an invalid value.

Last edited 2 years ago by russellm (previous) (diff)
Note: See TracTickets for help on using tickets.
Back to Top