diff --git a/CHANGELOG.txt b/CHANGELOG.txt index 986385d03..5e1d3fa8e 100644 --- a/CHANGELOG.txt +++ b/CHANGELOG.txt @@ -34,6 +34,7 @@ Changelog * Fix: Search no longer crashes when auto-indexing a model that doesn't have an id field (Scot Hacker) * Fix: The `wagtailfrontendcache` module's HTTP backend has been rewritten to reliably direct requests to the configured cache hostname * Fix: Resizing single pixel images with the "fill" filter no longer raises "ZeroDivisionError" or "tile cannot extend outside image" + * Fix: The queryset returned from `search` operations when using the database search backend now correctly preserves additional properties of the original query, such as `prefetch_related` / `select_related` 1.0 (16.07.2015) diff --git a/docs/releases/1.1.rst b/docs/releases/1.1.rst index 0ed3c4c9e..1ec77917a 100644 --- a/docs/releases/1.1.rst +++ b/docs/releases/1.1.rst @@ -75,6 +75,7 @@ Bug fixes * Search no longer crashes when auto-indexing a model that doesn't have an ``id`` field * The ``wagtailfrontendcache`` module's HTTP backend has been rewritten to reliably direct requests to the configured cache hostname * Resizing single pixel images with the "fill" filter no longer raises "ZeroDivisionError" or "tile cannot extend outside image" + * The queryset returned from ``search`` operations when using the database search backend now correctly preserves additional properties of the original query, such as ``prefetch_related`` / ``select_related`` Upgrade considerations diff --git a/wagtail/wagtailcore/tests/test_page_queryset.py b/wagtail/wagtailcore/tests/test_page_queryset.py index 9c4f3e7b7..c9913bc9c 100644 --- a/wagtail/wagtailcore/tests/test_page_queryset.py +++ b/wagtail/wagtailcore/tests/test_page_queryset.py @@ -410,3 +410,17 @@ class TestSpecificQuery(TestCase): Page.objects.get(url_path='/home/events/christmas/').specific, Page.objects.get(url_path='/home/events/').specific, Page.objects.get(url_path='/home/about-us/').specific]) + + def test_specific_query_with_search(self): + # 1276 - The database search backend didn't return results with the + # specific type when searching a specific queryset. + + pages = list(Page.objects.specific().live().in_menu().search(None, backend='wagtail.wagtailsearch.backends.db')) + + # Check that each page is in the queryset with the correct type. + # We don't care about order here + self.assertEqual(len(pages), 4) + self.assertIn(Page.objects.get(url_path='/home/other/').specific, pages) + self.assertIn(Page.objects.get(url_path='/home/events/christmas/').specific, pages) + self.assertIn(Page.objects.get(url_path='/home/events/').specific, pages) + self.assertIn(Page.objects.get(url_path='/home/about-us/').specific, pages) diff --git a/wagtail/wagtailsearch/backends/db.py b/wagtail/wagtailsearch/backends/db.py index 2e789973e..e7e32fce0 100644 --- a/wagtail/wagtailsearch/backends/db.py +++ b/wagtail/wagtailsearch/backends/db.py @@ -22,10 +22,12 @@ class DBSearchQuery(BaseSearchQuery): return q - def get_q(self): - # Get filters as a q object - q = self._get_filters_from_queryset() + def get_extra_q(self): + # Run _get_filters_from_queryset to test that no fields that are not + # a FilterField have been used in the query. + self._get_filters_from_queryset() + q = models.Q() model = self.queryset.model if self.query_string is not None: @@ -57,10 +59,10 @@ class DBSearchQuery(BaseSearchQuery): class DBSearchResults(BaseSearchResults): def get_queryset(self): - model = self.query.queryset.model - q = self.query.get_q() + queryset = self.query.queryset + q = self.query.get_extra_q() - return model.objects.filter(q).distinct()[self.start:self.stop] + return queryset.filter(q).distinct()[self.start:self.stop] def _do_search(self): return self.get_queryset()