summaryrefslogtreecommitdiff
path: root/django/contrib/gis
diff options
context:
space:
mode:
authorJacob Walls <jwalls@azavea.com>2023-02-02 10:23:16 -0500
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2023-02-07 11:48:10 +0100
commit4403432b759124aa613249373e0d2ede64ae8765 (patch)
tree120b68e4926f48b961f302a1b064066864d68daa /django/contrib/gis
parentfb77be9ae1cbca4a52948e129c32a482c2c99c10 (diff)
downloaddjango-4403432b759124aa613249373e0d2ede64ae8765.tar.gz
Fixed #33638 -- Fixed GIS lookups crash with geography fields on PostGIS.
Diffstat (limited to 'django/contrib/gis')
-rw-r--r--django/contrib/gis/db/backends/postgis/operations.py16
1 files changed, 9 insertions, 7 deletions
diff --git a/django/contrib/gis/db/backends/postgis/operations.py b/django/contrib/gis/db/backends/postgis/operations.py
index efa1d0f204..b68db377f8 100644
--- a/django/contrib/gis/db/backends/postgis/operations.py
+++ b/django/contrib/gis/db/backends/postgis/operations.py
@@ -27,7 +27,8 @@ BILATERAL = "bilateral"
class PostGISOperator(SpatialOperator):
def __init__(self, geography=False, raster=False, **kwargs):
# Only a subset of the operators and functions are available for the
- # geography type.
+ # geography type. Lookups that don't support geography will be cast to
+ # geometry.
self.geography = geography
# Only a subset of the operators and functions are available for the
# raster type. Lookups that don't support raster will be converted to
@@ -37,13 +38,8 @@ class PostGISOperator(SpatialOperator):
super().__init__(**kwargs)
def as_sql(self, connection, lookup, template_params, *args):
- if lookup.lhs.output_field.geography and not self.geography:
- raise ValueError(
- 'PostGIS geography does not support the "%s" '
- "function/operator." % (self.func or self.op,)
- )
-
template_params = self.check_raster(lookup, template_params)
+ template_params = self.check_geography(lookup, template_params)
return super().as_sql(connection, lookup, template_params, *args)
def check_raster(self, lookup, template_params):
@@ -93,6 +89,12 @@ class PostGISOperator(SpatialOperator):
return template_params
+ def check_geography(self, lookup, template_params):
+ """Convert geography fields to geometry types, if necessary."""
+ if lookup.lhs.output_field.geography and not self.geography:
+ template_params["lhs"] += "::geometry"
+ return template_params
+
class ST_Polygon(Func):
function = "ST_Polygon"