Ticket #15305: geoq_agg.patch

File geoq_agg.patch, 2.5 KB (added by milosu, 8 years ago)

patch and related test case

  • django/contrib/gis/db/models/sql/compiler.py

     
    171171        """
    172172        values = []
    173173        aliases = self.query.extra_select.keys()
    174         if self.query.aggregates:
    175             # If we have an aggregate annotation, must extend the aliases
    176             # so their corresponding row values are included.
    177             aliases.extend([None for i in xrange(len(self.query.aggregates))])
    178174
    179175        # Have to set a starting row number offset that is used for
    180176        # determining the correct starting row index -- needed for
     
    191187                               self.connection)
    192188                  for v, a in izip(row[rn_offset:index_start], aliases)]
    193189        if self.connection.ops.oracle or getattr(self.query, 'geo_values', False):
     190            aggregates_start = index_start + len(fields)
    194191            # We resolve the rest of the columns if we're on Oracle or if
    195192            # the `geo_values` attribute is defined.
    196             for value, field in map(None, row[index_start:], fields):
     193            for value, field in map(None, row[index_start:aggregates_start], fields):
    197194                values.append(self.query.convert_values(value, field, connection=self.connection))
     195            # Aggregates will be resolved by the caller via resolve_aggregate
     196            values.extend(row[aggregates_start:])
    198197        else:
    199198            values.extend(row[index_start:])
    200199        return tuple(values)
  • django/contrib/gis/tests/relatedapp/tests.py

     
    240240        # Count annotation should be 2 for the Dallas location now.
    241241        loc = Location.objects.annotate(num_cities=Count('city')).get(id=dallas.location.id)
    242242        self.assertEqual(2, loc.num_cities)
     243        loc_q = Location.objects.annotate(num_cities=Count('city'))
     244        self.assertEqual(5, len(loc_q))
     245        self.assertTrue(loc_q[0].num_cities > 0)
     246        loc_qv = Location.objects.all().values('id').annotate(num_cities=Count('city'))
     247        self.assertEqual(5, len(loc_qv))
     248        self.assertTrue(loc_qv[0]['num_cities'] > 0)
    243249
    244250    def test12b_count(self):
    245251        "Testing `Count` aggregate use with the `GeoManager` on non geo-fields. See #11087."
Back to Top