Code

Ticket #10159: geowherenode_expressions_fix.diff

File geowherenode_expressions_fix.diff, 1.7 KB (added by jbronn, 5 years ago)
Line 
1Index: django/contrib/gis/db/models/sql/where.py
2===================================================================
3--- django/contrib/gis/db/models/sql/where.py   (revision 9795)
4+++ django/contrib/gis/db/models/sql/where.py   (working copy)
5@@ -1,7 +1,10 @@
6 import datetime
7+from django.db import connection
8 from django.db.models.fields import Field
9+from django.db.models.sql.expressions import SQLEvaluator
10 from django.db.models.sql.where import WhereNode
11 from django.contrib.gis.db.backend import get_geo_where_clause, SpatialBackend
12+qn = connection.ops.quote_name
13 
14 class GeoAnnotation(object):
15     """
16@@ -37,10 +40,17 @@
17             # Not a geographic field, so call `WhereNode.add`.
18             return super(GeoWhereNode, self).add(data, connector)
19         else:
20-            # `GeometryField.get_db_prep_lookup` returns a where clause
21-            # substitution array in addition to the parameters.
22-            where, params = field.get_db_prep_lookup(lookup_type, value)
23 
24+            if isinstance(value, SQLEvaluator):
25+                # If an expression is used, we are getting a database column so
26+                # we we don't send to get_db_prep_lookup.
27+                where =  ['%s.%s' % tuple(map(qn, value.cols[value.expression]))]
28+                params = ()
29+            else:
30+                # `GeometryField.get_db_prep_lookup` returns a where clause
31+                # substitution array in addition to the parameters.
32+                where, params = field.get_db_prep_lookup(lookup_type, value)
33+
34             # The annotation will be a `GeoAnnotation` object that
35             # will contain the necessary geometry field metadata for
36             # the `get_geo_where_clause` to construct the appropriate