Ticket #28811: patchfor28811.diff

File patchfor28811.diff, 4.5 KB (added by Robin Ramael, 7 years ago)

patch

  • 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:  
    15131513                # that case we need to return a Ref to the subquery's annotation.
    15141514                return Ref(name, self.annotation_select[name])
    15151515            else:
    1516                 return self.annotation_select[name]
     1516                return self.annotations[name]
    15171517        else:
    15181518            field_list = name.split(LOOKUP_SEP)
    15191519            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):  
    704704
    705705    class Meta:
    706706        db_table = 'RelatedIndividual'
     707
     708
     709class 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  
    66
    77from django.core.exceptions import EmptyResultSet, FieldError
    88from django.db import DEFAULT_DB_ALIAS, connection
    9 from django.db.models import Count, F, Q
     9from django.db.models import Count, F, Q, Sum, Value
    1010from django.db.models.sql.constants import LOUTER
    1111from django.db.models.sql.where import NothingNode, WhereNode
    1212from django.test import TestCase, skipUnlessDBFeature
    from django.test.utils import CaptureQueriesContext  
    1515from .models import (
    1616    FK1, Annotation, Article, Author, BaseA, Book, CategoryItem,
    1717    CategoryRelationship, Celebrity, Channel, Chapter, Child, ChildObjectA,
    18     Classroom, Company, Cover, CustomPk, CustomPkTag, Detail, 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     Note, NullableName, Number, ObjectA, ObjectB, ObjectC, OneToOneCategory,
    23     Order, OrderItem, Page, Paragraph, Person, Plaything, PointerA, Program,
    24     ProxyCategory, ProxyObjectA, ProxyObjectB, Ranking, Related,
    25     RelatedIndividual, RelatedObject, Report, ReportComment, ReservedName,
    26     Responsibility, School, SharedConnection, SimpleCategory, SingleObject,
    27     SpecialCategory, Staff, StaffUser, Student, Tag, Task, Teacher,
    28     Ticket21203Child, 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,
    2929    Ticket23605C, TvChef, Valid, X,
    3030)
    3131
    class Ticket23622Tests(TestCase):  
    38633863            set(Ticket23605A.objects.filter(qy).values_list('pk', flat=True))
    38643864        )
    38653865        self.assertSequenceEqual(Ticket23605A.objects.filter(qx), [a2])
     3866
     3867
     3868class 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        )
Back to Top