Allow referencing models by string representation in SnippetChooserBlock

pull/3042/head
Nick Smith 2016-06-22 10:43:23 +01:00 zatwierdzone przez Matt Westcott
rodzic 4b516c0735
commit df9a5e31fa
4 zmienionych plików z 13 dodań i 1 usunięć

Wyświetl plik

@ -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)

Wyświetl plik

@ -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 <wagtailsearch_hits_max_age>` setting (Stephen Rice)
* ``SnippetChooserBlock`` now supports passing the model name as a string (Nick Smith)
Bug fixes

Wyświetl plik

@ -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):

Wyświetl plik

@ -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")