Ticket #28811: patchfor28811.diff
File patchfor28811.diff, 4.5 KB (added by , 7 years ago) |
---|
-
django/db/models/sql/query.py
diff --git a/django/db/models/sql/query.py b/django/db/models/sql/query.py index e7456de..088ffd1 100644
a b class Query: 1513 1513 # that case we need to return a Ref to the subquery's annotation. 1514 1514 return Ref(name, self.annotation_select[name]) 1515 1515 else: 1516 return self.annotation _select[name]1516 return self.annotations[name] 1517 1517 else: 1518 1518 field_list = name.split(LOOKUP_SEP) 1519 1519 join_info = self.setup_joins(field_list, self.get_meta(), self.get_initial_alias(), can_reuse=reuse) -
tests/queries/models.py
diff --git a/tests/queries/models.py b/tests/queries/models.py index c3f5125..7bc0fda 100644
a b class RelatedIndividual(models.Model): 704 704 705 705 class Meta: 706 706 db_table = 'RelatedIndividual' 707 708 709 class Data(models.Model): 710 option = models.CharField(max_length=2) 711 value = models.IntegerField() -
tests/queries/tests.py
diff --git a/tests/queries/tests.py b/tests/queries/tests.py index 668b073..3cb8208 100644
a b from operator import attrgetter 6 6 7 7 from django.core.exceptions import EmptyResultSet, FieldError 8 8 from django.db import DEFAULT_DB_ALIAS, connection 9 from django.db.models import Count, F, Q 9 from django.db.models import Count, F, Q, Sum, Value 10 10 from django.db.models.sql.constants import LOUTER 11 11 from django.db.models.sql.where import NothingNode, WhereNode 12 12 from django.test import TestCase, skipUnlessDBFeature … … from django.test.utils import CaptureQueriesContext 15 15 from .models import ( 16 16 FK1, Annotation, Article, Author, BaseA, Book, CategoryItem, 17 17 CategoryRelationship, Celebrity, Channel, Chapter, Child, ChildObjectA, 18 Classroom, Company, Cover, CustomPk, CustomPkTag, D etail, DumbCategory,19 Eaten, Employment, ExtraInfo, Fan, Food, Identifier, Individual, Item, Job,20 JobResponsibilities, Join, LeafA, LeafB, LoopX, LoopZ, ManagedModel,21 Member, ModelA, ModelB, ModelC, ModelD, MyObject, NamedCategory, Node,22 N ote, NullableName, Number, ObjectA, ObjectB, ObjectC, OneToOneCategory,23 O rder, OrderItem, Page, Paragraph, Person, Plaything, PointerA, Program,24 P roxyCategory, ProxyObjectA, ProxyObjectB, Ranking, Related,25 Related Individual, RelatedObject, Report, ReportComment, ReservedName,26 Res ponsibility, School, SharedConnection, SimpleCategory, SingleObject,27 S pecialCategory, Staff, StaffUser, Student, Tag, Task, Teacher,28 T icket21203Child, Ticket21203Parent, Ticket23605A, Ticket23605B,18 Classroom, Company, Cover, CustomPk, CustomPkTag, Data, Detail, 19 DumbCategory, Eaten, Employment, ExtraInfo, Fan, Food, Identifier, 20 Individual, Item, Job, JobResponsibilities, Join, LeafA, LeafB, LoopX, 21 LoopZ, ManagedModel, Member, ModelA, ModelB, ModelC, ModelD, MyObject, 22 NamedCategory, Node, Note, NullableName, Number, ObjectA, ObjectB, ObjectC, 23 OneToOneCategory, Order, OrderItem, Page, Paragraph, Person, Plaything, 24 PointerA, Program, ProxyCategory, ProxyObjectA, ProxyObjectB, Ranking, 25 Related, RelatedIndividual, RelatedObject, Report, ReportComment, 26 ReservedName, Responsibility, School, SharedConnection, SimpleCategory, 27 SingleObject, SpecialCategory, Staff, StaffUser, Student, Tag, Task, 28 Teacher, Ticket21203Child, Ticket21203Parent, Ticket23605A, Ticket23605B, 29 29 Ticket23605C, TvChef, Valid, X, 30 30 ) 31 31 … … class Ticket23622Tests(TestCase): 3863 3863 set(Ticket23605A.objects.filter(qy).values_list('pk', flat=True)) 3864 3864 ) 3865 3865 self.assertSequenceEqual(Ticket23605A.objects.filter(qx), [a2]) 3866 3867 3868 class Ticket28811Tests(TestCase): 3869 3870 def test_simple(self): 3871 3872 Data.objects.create(option='A', value=1) 3873 Data.objects.create(option='A', value=2) 3874 3875 Data.objects.create(option='B', value=3) 3876 Data.objects.create(option='B', value=4) 3877 3878 data_qs = (Data.objects 3879 .annotate(multiplier=Value(3)) # will of course be far more complex in the wild 3880 # group by option => sum of value * multiplier 3881 .values('option') 3882 .annotate(multiplied_value_sum=Sum(F('multiplier') * F('value'))) 3883 .order_by()) 3884 3885 self.assertCountEqual( 3886 data_qs, 3887 [{'option': 'A', 'multiplied_value_sum': (2 + 1) * 3}, 3888 {'option': 'B', 'multiplied_value_sum': (3 + 4) * 3}], 3889 )