the spatialproxy object allows for lazygeometries and lazyrasters the proxy uses python descriptors for instantiating and setting geometry or raster objects corresponding to geographic model fields thanks to robert coup for providing this functionality see 4322 initialize on the given geometry or raster class not an instance and the corresponding field retrieve the geometry or raster initializing it using the corresponding class specified during initialization and the value of the field currently geos or ogr geometries as well as gdalrasters are supported accessed on a class not an instance getting the value of the field otherwise a geometry or raster object is built using the field s contents and the model s corresponding attribute is set retrieve the proxied geometry or raster with the corresponding class specified during initialization to set geometries use values of none hexewkb or wkt to set rasters use json or dict values the geographic type of the field for raster fields ensure input is none or a string dict or raster instance the geometry type must match that of the field unless the general geometryfield is used assigning the field srid if the geometry has no srid set geometries with none wkt hex or wkb setting the objects dictionary with the value and returning initialize on the given geometry or raster class not an instance and the corresponding field retrieve the geometry or raster initializing it using the corresponding class specified during initialization and the value of the field currently geos or ogr geometries as well as gdalrasters are supported accessed on a class not an instance getting the value of the field otherwise a geometry or raster object is built using the field s contents and the model s corresponding attribute is set retrieve the proxied geometry or raster with the corresponding class specified during initialization to set geometries use values of none hexewkb or wkt to set rasters use json or dict values the geographic type of the field for raster fields ensure input is none or a string dict or raster instance the geometry type must match that of the field unless the general geometryfield is used assigning the field srid if the geometry has no srid set geometries with none wkt hex or wkb setting the objects dictionary with the value and returning
from django.db.models.query_utils import DeferredAttribute


class SpatialProxy(DeferredAttribute):
    def __init__(self, klass, field, load_func=None):
        
        self._klass = klass
        self._load_func = load_func or klass
        super().__init__(field)

    def __get__(self, instance, cls=None):
        
        if instance is None:
            
            return self

        
        try:
            geo_value = instance.__dict__[self.field.attname]
        except KeyError:
            geo_value = super().__get__(instance, cls)

        if isinstance(geo_value, self._klass):
            geo_obj = geo_value
        elif (geo_value is None) or (geo_value == ""):
            geo_obj = None
        else:
            
            
            geo_obj = self._load_func(geo_value)
            setattr(instance, self.field.attname, geo_obj)
        return geo_obj

    def __set__(self, instance, value):
        
        
        gtype = self.field.geom_type

        if gtype == "RASTER" and (
            value is None or isinstance(value, (str, dict, self._klass))
        ):
            
            
            pass
        elif isinstance(value, self._klass):
            
            
            if value.srid is None:
                
                value.srid = self.field.srid
        elif value is None or isinstance(value, (str, memoryview)):
            
            pass
        else:
            raise TypeError(
                "Cannot set %s SpatialProxy (%s) with value of type: %s"
                % (instance.__class__.__name__, gtype, type(value))
            )

        
        instance.__dict__[self.field.attname] = value
        return value