Skip to content

Conversation

@rodrigobnogueira
Copy link

@rodrigobnogueira rodrigobnogueira commented Dec 29, 2025

What does this PR do?

Problem

get_user_model().USERNAME_FIELD was called during axes/conf.py module import, causing circular import errors when custom user models imported from axes (e.g., from axes.utils import reset).
Error: ImproperlyConfigured: AUTH_USER_MODEL refers to model that has not been installed

Solution

Used Django's SimpleLazyObject to defer evaluation of get_user_model() until the value is actually accessed, breaking the circular dependency.

Changes

  • axes/conf.py:
    • Added SimpleLazyObject import
    • Created _get_username_field_default() helper
    • Wrapped default value in SimpleLazyObject
  • tests/test_conf.py: Added 2 tests for lazy evaluation

Testing

  • All 349 tests pass (347 existing + 2 new)
  • No regressions introduced
  • Verified fix resolves circular import with custom user models

Fixes #1280 - Circular import error when using custom user models that import from django-axes.

Checks

  • This PR fixes a typo or improves the docs (you can dismiss the other checks if that's the case).
  • Did you make sure to update the documentation with your changes?
  • Did you write any new necessary tests?

rodrigo.nogueira added 2 commits December 29, 2025 10:58
Fixes jazzband#1328
- Add None as default value in axes_conf_check
- Add test coverage for missing settings scenario
Fixes jazzband#1280

- Use SimpleLazyObject to defer get_user_model() evaluation
- Prevents circular import when custom user models import from axes
- Add test coverage for lazy evaluation in test_conf.py
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

1 participant