diff --git a/CHANGELOG.txt b/CHANGELOG.txt index b6474462fe3f..99b8d4e3b91c 100644 --- a/CHANGELOG.txt +++ b/CHANGELOG.txt @@ -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) diff --git a/docs/releases/6.2.1.md b/docs/releases/6.2.1.md index b976927fa56f..34ed31dba2f9 100644 --- a/docs/releases/6.2.1.md +++ b/docs/releases/6.2.1.md @@ -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) diff --git a/wagtail/admin/models.py b/wagtail/admin/models.py index d30beca107cf..1a7e69860db4 100644 --- a/wagtail/admin/models.py +++ b/wagtail/admin/models.py @@ -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 @@ -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", ) diff --git a/wagtail/test/customuser/models.py b/wagtail/test/customuser/models.py index 3e4b1472cd0d..1197bbcfd1be 100644 --- a/wagtail/test/customuser/models.py +++ b/wagtail/test/customuser/models.py @@ -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