Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUG: get_user_model().USERNAME_FIELD causing starting Django when using custom user model #1280

Open
daronzwink opened this issue Feb 20, 2025 · 1 comment
Labels

Comments

@daronzwink
Copy link

daronzwink commented Feb 20, 2025

Describe the bug
After installing 7.0.2, we are getting an error starting Django as we have Django configured to use a custom user model. Our custom user model is:

AUTH_USER_MODEL = "tenants.TenantUser"

We are receiving the following error:

Traceback (most recent call last):
File "/code/manage.py", line 32, in
main()
File "/code/manage.py", line 28, in main
execute_from_command_line(sys.argv)
File "/usr/local/lib/python3.11/site-packages/django/core/management/init.py", line 442, in execute_from_command_line
utility.execute()
File "/usr/local/lib/python3.11/site-packages/django/core/management/init.py", line 416, in execute
django.setup()
File "/usr/local/lib/python3.11/site-packages/django/init.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "/usr/local/lib/python3.11/site-packages/django/apps/registry.py", line 116, in populate
app_config.import_models()
File "/usr/local/lib/python3.11/site-packages/django/apps/config.py", line 269, in import_models
self.models_module = import_module(models_module_name)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/importlib/init.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "", line 1204, in _gcd_import
File "", line 1176, in _find_and_load
File "", line 1147, in _find_and_load_unlocked
File "", line 690, in _load_unlocked
File "", line 940, in exec_module
File "", line 241, in _call_with_frames_removed
File "/code/action_manager/models.py", line 4, in
from utils.models import AbstractItemModel
File "/code/utils/models.py", line 4, in
from tenants.models.tenant import Tenant
File "/code/tenants/models/init.py", line 10, in
from .tenant_user import (
File "/code/tenants/models/tenant_user.py", line 18, in
from axes.utils import reset
File "/usr/local/lib/python3.11/site-packages/axes/utils.py", line 13, in
from axes.handlers.proxy import AxesProxyHandler
File "/usr/local/lib/python3.11/site-packages/axes/handlers/proxy.py", line 10, in
from axes.conf import settings
File "/usr/local/lib/python3.11/site-packages/axes/conf.py", line 47, in
settings, "AXES_USERNAME_FORM_FIELD", get_user_model().USERNAME_FIELD
^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/django/contrib/auth/init.py", line 176, in get_user_model
raise ImproperlyConfigured(
django.core.exceptions.ImproperlyConfigured: AUTH_USER_MODEL refers to model 'tenants.TenantUser' that has not been installed

Just to clear up any questions, we do have our "tenants" in the Django INSTALLED_APPS and we also have our "TenantUser" model imported in the "tenants > models > __init__.py" file. Finally, we do have "USERNAME_FIELD" defined in our "TenatUser" model and it has a value of:

USERNAME_FIELD = "email"

To Reproduce
Steps to reproduce the behavior:

  1. Upgrade django-axes to 7.02
  2. Configure Django to use a custom model
  3. There should be an error attempting to boot Django similar to the trace above.

Expected behavior
If AXES_USERNAME_FORM_FIELD is defined, it should not attempt to resolve "get_user_model().USERNAME_FIELD", which is not able to directly resolve when using a custom model in its current state.

Your environment
python version: 3.11
django version: 4.2.19
django-axes version: 7.0.2

Additional context
As soon as I downgrade to 7.0.1, everything works properly. I believe it is related to recent fix #1270

Possible implementation
Check for the existence of AXES_USERNAME_FORM_FIELD before attempting to resolve get_user_model().USERNAME_FIELD, or if attempting to resolve get_user_model().USERNAME_FIELD, ensure USERNAME_FIELD returns a valid value.

@daronzwink daronzwink added the bug label Feb 20, 2025
@daronzwink daronzwink changed the title BUG: Short description of the problem BUG: get_user_model().USERNAME_FIELD causing starting Django when using custom user model Feb 20, 2025
@aliceni81
Copy link

+1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants