mirror of
https://github.com/django/django.git
synced 2024-12-23 01:25:58 +00:00
Fixed #33374 -- Fixed ExpressionWrapper annotations with full queryset.
This commit is contained in:
parent
03cadb912c
commit
72b23c04d8
@ -994,6 +994,15 @@ class BooleanField(Field):
|
|||||||
defaults = {'form_class': form_class, 'required': False}
|
defaults = {'form_class': form_class, 'required': False}
|
||||||
return super().formfield(**{**defaults, **kwargs})
|
return super().formfield(**{**defaults, **kwargs})
|
||||||
|
|
||||||
|
def select_format(self, compiler, sql, params):
|
||||||
|
sql, params = super().select_format(compiler, sql, params)
|
||||||
|
# Filters that match everything are handled as empty strings in the
|
||||||
|
# WHERE clause, but in SELECT or GROUP BY list they must use a
|
||||||
|
# predicate that's always True.
|
||||||
|
if sql == '':
|
||||||
|
sql = '1'
|
||||||
|
return sql, params
|
||||||
|
|
||||||
|
|
||||||
class CharField(Field):
|
class CharField(Field):
|
||||||
description = _("String (up to %(max_length)s)")
|
description = _("String (up to %(max_length)s)")
|
||||||
|
@ -210,6 +210,26 @@ class NonAggregateAnnotationTestCase(TestCase):
|
|||||||
self.assertEqual(len(books), Book.objects.count())
|
self.assertEqual(len(books), Book.objects.count())
|
||||||
self.assertTrue(all(not book.selected for book in books))
|
self.assertTrue(all(not book.selected for book in books))
|
||||||
|
|
||||||
|
def test_full_expression_annotation(self):
|
||||||
|
books = Book.objects.annotate(
|
||||||
|
selected=ExpressionWrapper(~Q(pk__in=[]), output_field=BooleanField()),
|
||||||
|
)
|
||||||
|
self.assertEqual(len(books), Book.objects.count())
|
||||||
|
self.assertTrue(all(book.selected for book in books))
|
||||||
|
|
||||||
|
def test_full_expression_annotation_with_aggregation(self):
|
||||||
|
qs = Book.objects.filter(isbn='159059725').annotate(
|
||||||
|
selected=ExpressionWrapper(~Q(pk__in=[]), output_field=BooleanField()),
|
||||||
|
rating_count=Count('rating'),
|
||||||
|
)
|
||||||
|
self.assertEqual([book.rating_count for book in qs], [1])
|
||||||
|
|
||||||
|
def test_aggregate_over_full_expression_annotation(self):
|
||||||
|
qs = Book.objects.annotate(
|
||||||
|
selected=ExpressionWrapper(~Q(pk__in=[]), output_field=BooleanField()),
|
||||||
|
).aggregate(Sum('selected'))
|
||||||
|
self.assertEqual(qs['selected__sum'], Book.objects.count())
|
||||||
|
|
||||||
def test_empty_queryset_annotation(self):
|
def test_empty_queryset_annotation(self):
|
||||||
qs = Author.objects.annotate(
|
qs = Author.objects.annotate(
|
||||||
empty=Subquery(Author.objects.values('id').none())
|
empty=Subquery(Author.objects.values('id').none())
|
||||||
|
Loading…
Reference in New Issue
Block a user