diff --git a/CHANGELOG.txt b/CHANGELOG.txt index 88503cdc98..f1e736a2cf 100644 --- a/CHANGELOG.txt +++ b/CHANGELOG.txt @@ -20,6 +20,7 @@ Changelog * Add ability to return HTML in multiple image upload errors (Gordon Pendleton) * Upgrade internal JS tooling; Node v14 plus other smaller package upgrades (LB (Ben Johnston)) * Add support for `non_field_errors` rendering in Workflow action modal (LB (Ben Johnston)) + * Support calling `get_image_model` and `get_document_model` at import time (Matt Westcott) * Fix: Delete button is now correct colour on snippets and modeladmin listings (Brandon Murch) * Fix: Ensure that StreamBlock / ListBlock-level validation errors are counted towards error counts (Matt Westcott) * Fix: InlinePanel add button is now keyboard navigatable (Jesse Menn) diff --git a/docs/releases/2.15.rst b/docs/releases/2.15.rst index 449acd79b7..211a8bf172 100644 --- a/docs/releases/2.15.rst +++ b/docs/releases/2.15.rst @@ -31,6 +31,7 @@ Other features * Add ability to return HTML in multiple image upload errors (Gordon Pendleton) * Upgrade internal JS tooling; Node v14 plus other smaller package upgrades (LB (Ben Johnston)) * Add support for ``non_field_errors`` rendering in Workflow action modal (LB (Ben Johnston)) + * Support calling ``get_image_model`` and ``get_document_model`` at import time (Matt Westcott) Bug fixes ~~~~~~~~~ diff --git a/wagtail/documents/__init__.py b/wagtail/documents/__init__.py index f4e3c6c18e..c0f15b06ac 100644 --- a/wagtail/documents/__init__.py +++ b/wagtail/documents/__init__.py @@ -23,7 +23,7 @@ def get_document_model(): from django.apps import apps model_string = get_document_model_string() try: - return apps.get_model(model_string) + return apps.get_model(model_string, require_ready=False) except ValueError: raise ImproperlyConfigured("WAGTAILDOCS_DOCUMENT_MODEL must be of the form 'app_label.model_name'") except LookupError: diff --git a/wagtail/documents/tests/test_models.py b/wagtail/documents/tests/test_models.py index f1f30a3d2d..cea246a74d 100644 --- a/wagtail/documents/tests/test_models.py +++ b/wagtail/documents/tests/test_models.py @@ -9,7 +9,7 @@ from django.test.utils import override_settings from wagtail.core.models import Collection, GroupCollectionPermission from wagtail.documents import get_document_model, get_document_model_string, models, signal_handlers from wagtail.images.tests.utils import get_test_image_file -from wagtail.tests.testapp.models import CustomDocument +from wagtail.tests.testapp.models import CustomDocument, ReimportedDocumentModel from wagtail.tests.utils import WagtailTestUtils @@ -232,6 +232,9 @@ class TestGetDocumentModel(WagtailTestUtils, TestCase): """Test get_document_model with a custom document model""" self.assertIs(get_document_model(), CustomDocument) + def test_get_document_model_at_import_time(self): + self.assertEqual(ReimportedDocumentModel, models.Document) + @override_settings(WAGTAILDOCS_DOCUMENT_MODEL='tests.CustomDocument') def test_custom_get_document_model_string(self): """Test get_document_model_string with a custom document model""" diff --git a/wagtail/images/__init__.py b/wagtail/images/__init__.py index 50a312812c..3a0069a912 100644 --- a/wagtail/images/__init__.py +++ b/wagtail/images/__init__.py @@ -24,7 +24,7 @@ def get_image_model(): from django.apps import apps model_string = get_image_model_string() try: - return apps.get_model(model_string) + return apps.get_model(model_string, require_ready=False) except ValueError: raise ImproperlyConfigured("WAGTAILIMAGES_IMAGE_MODEL must be of the form 'app_label.model_name'") except LookupError: diff --git a/wagtail/images/tests/test_models.py b/wagtail/images/tests/test_models.py index 4418337331..c09fb7a35a 100644 --- a/wagtail/images/tests/test_models.py +++ b/wagtail/images/tests/test_models.py @@ -12,7 +12,7 @@ from willow.image import Image as WillowImage from wagtail.core.models import Collection, GroupCollectionPermission, Page from wagtail.images.models import Rendition, SourceImageIOError from wagtail.images.rect import Rect -from wagtail.tests.testapp.models import EventPage, EventPageCarouselItem +from wagtail.tests.testapp.models import EventPage, EventPageCarouselItem, ReimportedImageModel from wagtail.tests.utils import WagtailTestUtils from .utils import Image, get_test_image_file @@ -26,6 +26,9 @@ class TestImage(TestCase): file=get_test_image_file(colour='white'), ) + def test_get_image_model_at_import_time(self): + self.assertEqual(ReimportedImageModel, Image) + def test_is_portrait(self): self.assertFalse(self.image.is_portrait()) diff --git a/wagtail/tests/testapp/models.py b/wagtail/tests/testapp/models.py index 1ef0166070..06a82f401a 100644 --- a/wagtail/tests/testapp/models.py +++ b/wagtail/tests/testapp/models.py @@ -35,8 +35,10 @@ from wagtail.core.blocks import ( CharBlock, FieldBlock, RawHTMLBlock, RichTextBlock, StreamBlock, StructBlock) from wagtail.core.fields import RichTextField, StreamField from wagtail.core.models import Orderable, Page, PageManager, PageQuerySet, Task, TranslatableMixin +from wagtail.documents import get_document_model from wagtail.documents.edit_handlers import DocumentChooserPanel from wagtail.documents.models import AbstractDocument, Document +from wagtail.images import get_image_model from wagtail.images.blocks import ImageChooserBlock from wagtail.images.edit_handlers import ImageChooserPanel from wagtail.images.models import AbstractImage, AbstractRendition, Image @@ -1571,3 +1573,9 @@ class DeadlyStreamPage(Page): content_panels = Page.content_panels + [ StreamFieldPanel('body'), ] + + +# Check that get_image_model and get_document_model work at import time +# (so that it's possible to use them in foreign key definitions, for example) +ReimportedImageModel = get_image_model() +ReimportedDocumentModel = get_document_model()