Skip to content

Commit

Permalink
Corrected FilterView behaviour with unbound FilterSet. (carltongibson…
Browse files Browse the repository at this point in the history
  • Loading branch information
carltongibson authored Jan 20, 2019
1 parent 99ced9c commit 0cbb723
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 2 deletions.
2 changes: 1 addition & 1 deletion django_filters/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ def get(self, request, *args, **kwargs):
filterset_class = self.get_filterset_class()
self.filterset = self.get_filterset(filterset_class)

if self.filterset.is_valid() or not self.get_strict():
if not self.filterset.is_bound or self.filterset.is_valid() or not self.get_strict():
self.object_list = self.filterset.qs
else:
self.object_list = self.filterset.queryset.none()
Expand Down
2 changes: 1 addition & 1 deletion tests/templates/tests/book_filter.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{{ filter.form }}

{% for obj in filter.qs %}
{% for obj in object_list %}
{{ obj }}
{% endfor %}
13 changes: 13 additions & 0 deletions tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,19 @@ class View(FilterView):
self.assertEqual(message, expected)
self.assertEqual(len(recorded), 0)

def test_view_with_unbound_filter_form_returns_initial_queryset(self):
factory = RequestFactory()
request = factory.get(self.base_url)

queryset = Book.objects.filter(title='Snowcrash')
view = FilterView.as_view(model=Book, queryset=queryset)

response = view(request)
titles = [o.title for o in response.context_data['object_list']]

self.assertEqual(response.status_code, 200)
self.assertEqual(titles, ['Snowcrash'])


class GenericFunctionalViewTests(GenericViewTestCase):
base_url = '/books-legacy/'
Expand Down

0 comments on commit 0cbb723

Please sign in to comment.