Skip to content

Commit

Permalink
Fix issue of get_queryset not working without explicit model
Browse files Browse the repository at this point in the history
  • Loading branch information
yaakovLowenstein committed Aug 7, 2024
1 parent 4a3eb8b commit 4fe476e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
8 changes: 8 additions & 0 deletions django_filters/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,14 @@ def get_filterset_kwargs(self, filterset_class):
)
args = (filterset_class.__name__, self.__class__.__name__)
raise ImproperlyConfigured(msg % args)
else:
self.model = filterset_class._meta.model
kwargs.update(
{
"queryset": self.get_queryset(),
}
)

return kwargs

def get_strict(self):
Expand Down
17 changes: 16 additions & 1 deletion tests/test_views.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from django.core.exceptions import ImproperlyConfigured
from django.db.models.query import QuerySet
from django.test import TestCase, override_settings
from django.test.client import RequestFactory
from django.utils import html
Expand Down Expand Up @@ -120,7 +121,21 @@ def test_view_with_unbound_filter_form_returns_initial_queryset(self):

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


def test_get_queryset_works_without_explicit_model(self):

class TestView(FilterView):
filterset_class = filterset_factory(Book)
def get_queryset(self):
qs= super().get_queryset()
qs=qs.filter(title="Snowcrash")
return qs

factory = RequestFactory()
request = factory.get(self.base_url)
view = TestView.as_view()
response = view(request)
assert response.context_data["object_list"].count() == 1

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

0 comments on commit 4fe476e

Please sign in to comment.