From df9a5e31fa1c6c64c962244161812270a2813f82 Mon Sep 17 00:00:00 2001 From: Nick Smith Date: Wed, 22 Jun 2016 10:43:23 +0100 Subject: [PATCH] Allow referencing models by string representation in SnippetChooserBlock --- CHANGELOG.txt | 1 + docs/releases/1.7.rst | 1 + wagtail/wagtailsnippets/blocks.py | 7 ++++++- wagtail/wagtailsnippets/tests.py | 5 +++++ 4 files changed, 13 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.txt b/CHANGELOG.txt index 3cd06b7fd4..8df7eaea24 100644 --- a/CHANGELOG.txt +++ b/CHANGELOG.txt @@ -17,6 +17,7 @@ Changelog * Added ability to annotate search results with score (Karl Hobley) * Added ability to limit access to form submissions (Mikalai Radchuk) * Added the ability to configure the number of days search logs are kept for (Stephen Rice) + * `SnippetChooserBlock` now supports passing the model name as a string (Nick Smith) * Fix: Migrations for wagtailcore and project template are now reversible (Benjamin Bach) * Fix: Migrations no longer depend on wagtailcore and taggit's `__latest__` migration, logically preventing those apps from receiving new migrations (Matt Westcott) * Fix: The default image format label text ('Full width', 'Left-aligned', 'Right-aligned') is now localised (Mikalai Radchuk) diff --git a/docs/releases/1.7.rst b/docs/releases/1.7.rst index 9e47b459ce..173c0a7795 100644 --- a/docs/releases/1.7.rst +++ b/docs/releases/1.7.rst @@ -47,6 +47,7 @@ Minor features * Added ability to annotate search results with score - see :ref:`wagtailsearch_annotating_results_with_score` (Karl Hobley) * Added ability to limit access to form submissions - see :ref:`filter_form_submissions_for_user` (Mikalai Radchuk) * Added the ability to configure the number of days search logs are kept for, through the :ref:`WAGTAILSEARCH_HITS_MAX_AGE ` setting (Stephen Rice) + * ``SnippetChooserBlock`` now supports passing the model name as a string (Nick Smith) Bug fixes diff --git a/wagtail/wagtailsnippets/blocks.py b/wagtail/wagtailsnippets/blocks.py index 54b89e761f..59024f0c07 100644 --- a/wagtail/wagtailsnippets/blocks.py +++ b/wagtail/wagtailsnippets/blocks.py @@ -3,12 +3,17 @@ from __future__ import absolute_import, unicode_literals from django.utils.functional import cached_property from wagtail.wagtailcore.blocks import ChooserBlock +from wagtail.wagtailcore.utils import resolve_model_string class SnippetChooserBlock(ChooserBlock): def __init__(self, target_model, **kwargs): super(SnippetChooserBlock, self).__init__(**kwargs) - self.target_model = target_model + self._target_model = target_model + + @cached_property + def target_model(self): + return resolve_model_string(self._target_model) @cached_property def widget(self): diff --git a/wagtail/wagtailsnippets/tests.py b/wagtail/wagtailsnippets/tests.py index 84d3125df6..292791361b 100644 --- a/wagtail/wagtailsnippets/tests.py +++ b/wagtail/wagtailsnippets/tests.py @@ -690,6 +690,11 @@ class TestSnippetChooserBlock(TestCase): # None should deserialize to None self.assertEqual(block.to_python(None), None) + def test_reference_model_by_string(self): + block = SnippetChooserBlock('tests.Advert') + test_advert = Advert.objects.get(text='test_advert') + self.assertEqual(block.to_python(test_advert.id), test_advert) + def test_form_render(self): block = SnippetChooserBlock(Advert, help_text="pick an advert, any advert")