mirror of
https://github.com/django/django.git
synced 2025-08-03 02:23:12 +00:00
Fixed #11293 -- fixed using Q objects to generate ORs with aggregates.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@15173 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
1b90cdcf23
commit
29de7ee9cb
2 changed files with 90 additions and 16 deletions
|
@ -4,8 +4,8 @@ from decimal import Decimal
|
|||
from operator import attrgetter
|
||||
|
||||
from django.core.exceptions import FieldError
|
||||
from django.db.models import Count, Max, Avg, Sum, StdDev, Variance, F, Q
|
||||
from django.test import TestCase, Approximate, skipUnlessDBFeature
|
||||
from django.db.models import Count, Max, Avg, Sum, StdDev, Variance, F
|
||||
|
||||
from models import Author, Book, Publisher, Clues, Entries, HardbackBook
|
||||
|
||||
|
@ -673,6 +673,58 @@ class AggregationTests(TestCase):
|
|||
list(qs), list(Book.objects.values_list("name", flat=True))
|
||||
)
|
||||
|
||||
def test_annotation_disjunction(self):
|
||||
qs = Book.objects.annotate(n_authors=Count("authors")).filter(
|
||||
Q(n_authors=2) | Q(name="Python Web Development with Django")
|
||||
)
|
||||
self.assertQuerysetEqual(
|
||||
qs, [
|
||||
"Artificial Intelligence: A Modern Approach",
|
||||
"Python Web Development with Django",
|
||||
"The Definitive Guide to Django: Web Development Done Right",
|
||||
],
|
||||
attrgetter("name")
|
||||
)
|
||||
|
||||
qs = Book.objects.annotate(n_authors=Count("authors")).filter(
|
||||
Q(name="The Definitive Guide to Django: Web Development Done Right") | (Q(name="Artificial Intelligence: A Modern Approach") & Q(n_authors=3))
|
||||
)
|
||||
self.assertQuerysetEqual(
|
||||
qs, [
|
||||
"The Definitive Guide to Django: Web Development Done Right",
|
||||
],
|
||||
attrgetter("name")
|
||||
)
|
||||
|
||||
qs = Publisher.objects.annotate(
|
||||
rating_sum=Sum("book__rating"),
|
||||
book_count=Count("book")
|
||||
).filter(
|
||||
Q(rating_sum__gt=5.5) | Q(rating_sum__isnull=True)
|
||||
).order_by('pk')
|
||||
self.assertQuerysetEqual(
|
||||
qs, [
|
||||
"Apress",
|
||||
"Prentice Hall",
|
||||
"Jonno's House of Books",
|
||||
],
|
||||
attrgetter("name")
|
||||
)
|
||||
|
||||
qs = Publisher.objects.annotate(
|
||||
rating_sum=Sum("book__rating"),
|
||||
book_count=Count("book")
|
||||
).filter(
|
||||
Q(pk__lt=F("book_count")) | Q(rating_sum=None)
|
||||
).order_by("pk")
|
||||
self.assertQuerysetEqual(
|
||||
qs, [
|
||||
"Apress",
|
||||
"Jonno's House of Books",
|
||||
],
|
||||
attrgetter("name")
|
||||
)
|
||||
|
||||
@skipUnlessDBFeature('supports_stddev')
|
||||
def test_stddev(self):
|
||||
self.assertEqual(
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue