diff --git a/cast/filters.py b/cast/filters.py index ddcfce95..132082dd 100644 --- a/cast/filters.py +++ b/cast/filters.py @@ -19,7 +19,7 @@ from django.utils.safestring import SafeText, mark_safe from django.utils.translation import gettext as _ from django_filters.fields import ChoiceField as FilterChoiceField -from wagtail.models import PageQuerySet +from wagtail.models import Page, PageQuerySet from cast import appsettings from cast.models.pages import PostTag @@ -328,9 +328,11 @@ def __init__( *, fetch_facet_counts: bool = False, ): - super().__init__(data=data, queryset=queryset) if data is None: data = QueryDict("") + if queryset is None: + queryset = Page.objects.none() + super().__init__(data=data, queryset=queryset) # Remove filters which are not configured in the settings configured_filters = set(appsettings.CAST_FILTERSET_FACETS) for filter_name in self.filters.copy().keys(): diff --git a/tests/filter_test.py b/tests/filter_test.py index ed638bc2..31c4fcec 100644 --- a/tests/filter_test.py +++ b/tests/filter_test.py @@ -98,6 +98,14 @@ def test_only_filter_specified_fields(field_name, facet_count_key): @pytest.mark.django_db class TestPostFilterset: + def test_data_is_none(self): + filterset = PostFilterset(None) + assert filterset.data == QueryDict("") + + def test_queryset_is_none(self): + filterset = PostFilterset(None, queryset=None) + assert filterset.qs.count() == 0 + def test_no_posts_no_date_facets(self): # given a filterset with no posts queryset = Post.objects.none()