Skip to content

Commit

Permalink
add support for qs methods
Browse files Browse the repository at this point in the history
  • Loading branch information
lyf2000 committed Jun 19, 2024
1 parent 0ee59a9 commit cab5aa4
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 13 deletions.
1 change: 0 additions & 1 deletion django/db/models/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -1732,7 +1732,6 @@ def extra(
select_params=None,
):
"""Add extra SQL fragments to the query."""
self._not_support_combined_queries("extra")
if self.query.is_sliced:
raise TypeError("Cannot change a query once a slice has been taken.")
clone = self._chain()
Expand Down
27 changes: 15 additions & 12 deletions tests/queries/test_qs_combinators.py
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,8 @@ def test_union_with_values_list_and_order_on_annotation(self):
qs1 = Number.objects.annotate(
annotation=Value(-1),
multiplier=F("annotation"),
).filter(num__gte=6)
).filter(num__gte=6).order_by('annotation')
list(qs1)
qs2 = Number.objects.annotate(
annotation=Value(2),
multiplier=F("annotation"),
Expand Down Expand Up @@ -633,15 +634,6 @@ def test_unsupported_operations_on_combined_qs(self):
combinators.append("intersection")
for combinator in combinators:
for operation in (
"alias",
"annotate",
"defer",
"delete",
"distinct",
"exclude",
"extra",
"filter",
"only",
"prefetch_related",
"select_related",
"update",
Expand Down Expand Up @@ -698,7 +690,7 @@ def test_filter_union(self):
qs1 = Number.objects.filter(pk__lt=4)
qs2 = Number.objects.filter(pk__gt=8)
qs = qs1.union(qs2).filter(pk__gte=3, pk__lte=9)
self.assertEqual(set(qs), set(Number.objects.filter(pk__in=(3,9))))
self.assertEqual(set(qs), set(Number.objects.filter(pk__in=(3, 9))))

def test_filter_union_multiple_models_same_values_list(self):
[Celebrity.objects.create(name=str(i)) for i in range(10)]
Expand Down Expand Up @@ -727,7 +719,6 @@ def test_filter_union_multiple_models_same_values_list(self):
# self.assertEqual(len(qs1.union(qs2).filter(pk__gte=3, pk__lte=9)), 2)

def test_filter_union_different_values_list_num(self):
[Celebrity.objects.create(name=str(i)) for i in range(10)]
qs1 = Number.objects.filter(pk__lt=4).values_list('pk')
qs2 = Number.objects.filter(pk__gt=8).values_list('pk', 'num')
msg = "SELECTs to the left and right of UNION do not have the same number of result columns"

Check failure on line 724 in tests/queries/test_qs_combinators.py

View workflow job for this annotation

GitHub Actions / flake8

line too long (100 > 88 characters)
Expand All @@ -749,3 +740,15 @@ def test_filter_union_multiple_models_different_values_list(self):
msg = "SELECTs of union do not have the same column names"
with self.assertRaisesMessage(ValueError, msg):
list(qs1.union(qs2))

def test_filter_intersection(self):
qs1 = Number.objects.filter(pk__lt=6)
qs2 = Number.objects.filter(pk__gt=2)
qs = qs1.intersection(qs2).filter(pk__gte=4)
self.assertEqual(set(qs), set(Number.objects.filter(pk__in=(4, 5))))

def test_filter_difference(self):
qs1 = Number.objects.filter(pk__lt=8)
qs2 = Number.objects.filter(pk__gt=4)
qs = qs1.difference(qs2).filter(pk__gte=2)
self.assertEqual(set(qs), set(Number.objects.filter(pk__in=(2, 3, 4))))

0 comments on commit cab5aa4

Please sign in to comment.