From e0c3a804454eb4fe48ac8a6ae83d4991a7e823f7 Mon Sep 17 00:00:00 2001 From: jpic Date: Wed, 18 Nov 2020 15:08:12 +0100 Subject: [PATCH] Support .filter(Q(...)) --- queryset_sequence/__init__.py | 4 ++-- tests/test_querysetsequence.py | 13 ++++++++++++- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/queryset_sequence/__init__.py b/queryset_sequence/__init__.py index c9d7077..3241780 100644 --- a/queryset_sequence/__init__.py +++ b/queryset_sequence/__init__.py @@ -685,12 +685,12 @@ def _filter_or_exclude_querysets(self, negate, **kwargs): self._querysets = [self._querysets[i] for i in self._queryset_idxs] # Methods that return new QuerySets - def filter(self, **kwargs): + def filter(self, *args, **kwargs): qss_fields, fields = self._separate_filter_fields(**kwargs) clone = self._clone() clone._filter_or_exclude_querysets(False, **qss_fields) - clone._querysets = [qs.filter(**fields) for qs in clone._querysets] + clone._querysets = [qs.filter(*args, **fields) for qs in clone._querysets] return clone def exclude(self, **kwargs): diff --git a/tests/test_querysetsequence.py b/tests/test_querysetsequence.py index 5911de9..71e549e 100644 --- a/tests/test_querysetsequence.py +++ b/tests/test_querysetsequence.py @@ -8,7 +8,7 @@ FieldError, MultipleObjectsReturned, ObjectDoesNotExist) -from django.db.models import Count +from django.db.models import Count, Q from django.db.models.query import EmptyQuerySet from django.test import TestCase @@ -443,6 +443,17 @@ def test_filter_by_relation(self): with self.assertNumQueries(2): self.assertEqual(bob_qss.count(), 3) + def test_filter_args(self): + """ + Ensure that filter() properly filters the children QuerySets, even when + being passed args. + """ + # Filter to just Bob's work. + with self.assertNumQueries(0): + bob_qss = self.all.filter(Q(author=self.bob)) + with self.assertNumQueries(2): + self.assertEqual(bob_qss.count(), 3) + def test_empty(self): """ Ensure that filter() works when it results in an empty QuerySet.