Skip to content

Commit a4f9ef4

Browse files
sir-sigurdtimgraham
authored andcommitted
Refs #28518 -- Improved performance of assigning values to GeometryFields.
1 parent c69e4bc commit a4f9ef4

File tree

2 files changed

+5
-4
lines changed

2 files changed

+5
-4
lines changed

django/contrib/gis/db/models/fields.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ def contribute_to_class(self, cls, name, **kwargs):
247247
super().contribute_to_class(cls, name, **kwargs)
248248

249249
# Setup for lazy-instantiated Geometry object.
250-
setattr(cls, self.attname, SpatialProxy(GEOSGeometry, self))
250+
setattr(cls, self.attname, SpatialProxy(self.geom_class or GEOSGeometry, self, load_func=GEOSGeometry))
251251

252252
def formfield(self, **kwargs):
253253
defaults = {'form_class': self.form_class,

django/contrib/gis/db/models/proxy.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,14 @@
99

1010

1111
class SpatialProxy(DeferredAttribute):
12-
def __init__(self, klass, field):
12+
def __init__(self, klass, field, load_func=None):
1313
"""
1414
Initialize on the given Geometry or Raster class (not an instance)
1515
and the corresponding field.
1616
"""
1717
self._field = field
1818
self._klass = klass
19+
self._load_func = load_func or klass
1920
super().__init__(field.attname, klass)
2021

2122
def __get__(self, instance, cls=None):
@@ -42,7 +43,7 @@ def __get__(self, instance, cls=None):
4243
else:
4344
# Otherwise, a geometry or raster object is built using the field's
4445
# contents, and the model's corresponding attribute is set.
45-
geo_obj = self._klass(geo_value)
46+
geo_obj = self._load_func(geo_value)
4647
setattr(instance, self._field.attname, geo_obj)
4748
return geo_obj
4849

@@ -61,7 +62,7 @@ def __set__(self, instance, value):
6162
# For raster fields, assure input is None or a string, dict, or
6263
# raster instance.
6364
pass
64-
elif isinstance(value, self._klass) and (str(value.geom_type).upper() == gtype or gtype == 'GEOMETRY'):
65+
elif isinstance(value, self._klass):
6566
# The geometry type must match that of the field -- unless the
6667
# general GeometryField is used.
6768
if value.srid is None:

0 commit comments

Comments
 (0)