From df08f999453dc02a674bec93937a38cacc4e4d79 Mon Sep 17 00:00:00 2001 From: Matt Westcott Date: Wed, 14 Aug 2024 19:50:11 +0100 Subject: [PATCH] Avoid importing custom user models at load time in wagtail.admin.models 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 #12228 (unconfirmed) --- CHANGELOG.txt | 1 + docs/releases/6.2.1.md | 1 + wagtail/admin/models.py | 4 ++-- wagtail/test/customuser/models.py | 9 ++++++--- 4 files changed, 10 insertions(+), 5 deletions(-) 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