Exception of the day – AUTH_USER_MODEL must be of the form ‘app_label.model_name’
TL;DR: if your AUTH_USER_MODEL
setting is correct, the most likely cause for this exception is that an exception is raised during import of the model.py
containing your custom user class.
I just encountered an exception that had me scratching my head for a minute or two.
Here is the relevant part for the stack trace:
File "/Users/danielhepper/Projects/myproject/models.py", line 89, in class TimestampedModelMixin(models.Model): File "/Users/danielhepper/Projects/myproject/models.py", line 93, in TimestampedModelMixin get_user_model(), null=True, on_delete=models.SET_NULL File "/Users/danielhepper/.virtualenvs/myproject-0SVSbvza/lib/python3.7/site-packages/django/contrib/auth/__init__.py", line 158, in get_user_model raise ImproperlyConfigured("AUTH_USER_MODEL must be of the form 'app_label.model_name'") django.core.exceptions.ImproperlyConfigured: AUTH_USER_MODEL must be of the form 'app_label.model_name'
Now, the project in question is using a custom user model, so I’ve set AUTH_USER_MODEL = "user.User"
, which is correct.
My first step was to look at the code that raises this exception in django/contrib/auth/__init__.py
:
try:
return django_apps.get_model(settings.AUTH_USER_MODEL, require_ready=False)
except ValueError:
raise ImproperlyConfigured("AUTH_USER_MODEL must be of the form 'app_label.model_name'")
Instead of digging deeper into the Django source code I took the pragmatic approach and temporarily commented out the exception block. And lo and behold, now I get an exception that is much more to the point:
ValueError: Signal receivers must accept keyword arguments (**kwargs
).
File "/Users/danielhepper/Epicco/Projects/myproject/users/models.py", line 24, in
@receiver(user_logged_in)
File "/Users/danielhepper/.virtualenvs/myproject-0SVSbvza/lib/python3.7/site-packages/django/dispatch/dispatcher.py", line 290, in _decorator
signal.connect(func, **kwargs) File "/Users/danielhepper/.virtualenvs/myproject-0SVSbvza/lib/python3.7/site-packages/django/dispatch/dispatcher.py", line 90, in connect raise ValueError("Signal receivers must accept keyword arguments (**kwargs).")
ValueError: Signal receivers must accept keyword arguments (**kwargs).
I’ve just added a signal receiver to the models.py file and forgot to add **kwargs
to the argument list. This leads to a ValueError being raised when the models.py
file gets imported.
Adding the missing **kwargs
fixed the exception. Furthermore, I moved the signal receivers to a separate signals.py
module, which gets imported in the ready()
method of the AppConfig
class, as is suggested in the Django docs on signals.