Skip to content

Commit

Permalink
Avoid importing custom user models at load time in wagtail.admin.models
Browse files Browse the repository at this point in the history
As per https://docs.djangoproject.com/en/5.0/topics/auth/customizing/#referencing-the-user-model , module-level code such as ForeignKey definitions should use `AUTH_USER_MODEL` rather than `get_user_model()`.

Probably fixes wagtail#12228 (unconfirmed)
  • Loading branch information
gasman committed Aug 15, 2024
1 parent bd87ccf commit df08f99
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ Changelog

* Fix: Handle `child_block` being passed as a kwarg in ListBlock migrations (Matt Westcott)
* Fix: Fix broken task type filter in workflow task chooser modal (Sage Abdullah)
* Fix: Prevent circular imports between `wagtail.admin.models` and custom user models (Matt Westcott)


6.2 (01.08.2024)
Expand Down
1 change: 1 addition & 0 deletions docs/releases/6.2.1.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ depth: 1

* Handle `child_block` being passed as a kwarg in ListBlock migrations (Matt Westcott)
* Fix broken task type filter in workflow task chooser modal (Sage Abdullah)
* Prevent circular imports between `wagtail.admin.models` and custom user models (Matt Westcott)
4 changes: 2 additions & 2 deletions wagtail/admin/models.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from django.contrib.auth import get_user_model
from django.conf import settings
from django.contrib.contenttypes.fields import GenericForeignKey
from django.contrib.contenttypes.models import ContentType
from django.db import models
Expand Down Expand Up @@ -76,7 +76,7 @@ def popular_tags_for_model(model, count=10):

class EditingSession(models.Model):
user = models.ForeignKey(
get_user_model(),
settings.AUTH_USER_MODEL,
on_delete=models.CASCADE,
related_name="editing_sessions",
)
Expand Down
9 changes: 6 additions & 3 deletions wagtail/test/customuser/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@
)
from django.db import models

# make sure we can import wagtail.admin.auth here without triggering a circular import
# (which is easily done because it's dealing with django.contrib.auth views which depend
# on the user model)
# Custom user models are a common source of circular import errors, since the user model is often
# referenced in Wagtail core code that we may want to import here. To prevent this, Wagtail should
# avoid importing the user model at load time.
# wagtail.admin.auth and wagtail.admin.views.generic are imported here as these have been
# previously identified as sources of circular imports.
from wagtail.admin.auth import permission_denied # noqa: F401
from wagtail.admin.panels import FieldPanel
from wagtail.admin.views.generic import chooser as chooser_views # noqa: F401

from .fields import ConvertedValueField

Expand Down

0 comments on commit df08f99

Please sign in to comment.