Skip to content

Commit

Permalink
Corrected FilterView behaviour with unbound FilterSet.
Browse files Browse the repository at this point in the history
Closes #930, #987, #1007.
  • Loading branch information
carltongibson committed Jan 20, 2019
1 parent 99ced9c commit 0b732a7
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 2 deletions.
2 changes: 1 addition & 1 deletion django_filters/views.py
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
@@ -1,5 +1,5 @@
{{ filter.form }}

{% for obj in filter.qs %}
{% for obj in object_list %}
{{ obj }}
{% endfor %}
12 changes: 12 additions & 0 deletions tests/test_views.py
Expand Up @@ -123,6 +123,18 @@ 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 0b732a7

Please sign in to comment.