{"record":{"id":"0f908d300a9da3d6","repo":"django/django","slug":"cannot-set-s-spatialproxy-s-with-value-of-type","errorCode":null,"errorMessage":"Cannot set %s SpatialProxy (%s) with value of type: %s","messagePattern":"Cannot set (.+?) SpatialProxy \\((.+?)\\) with value of type: (.+?)","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"django/contrib/gis/db/models/proxy.py","lineNumber":82,"sourceCode":"        gtype = self.field.geom_type\n\n        if gtype == \"RASTER\" and (\n            value is None or isinstance(value, (str, dict, self._klass))\n        ):\n            # For raster fields, ensure input is None or a string, dict, or\n            # raster instance.\n            pass\n        elif isinstance(value, self._klass):\n            # The geometry type must match that of the field -- unless the\n            # general GeometryField is used.\n            if value.srid is None:\n                # Assigning the field SRID if the geometry has no SRID.\n                value.srid = self.field.srid\n        elif value is None or isinstance(value, (str, memoryview)):\n            # Set geometries with None, WKT, HEX, or WKB\n            pass\n        else:\n            raise TypeError(\n                \"Cannot set %s SpatialProxy (%s) with value of type: %s\"\n                % (instance.__class__.__name__, gtype, type(value))\n            )\n\n        # Setting the object's dictionary with the value, and returning.\n        instance.__dict__[self.field.attname] = value\n        return value\n","sourceCodeStart":64,"sourceCodeEnd":90,"githubUrl":"https://github.com/django/django/blob/ae25a40be07e8a749edf526df37c93e59d4a22c9/django/contrib/gis/db/models/proxy.py#L64-L90","documentation":"Raised as TypeError by SpatialProxy.__set__ (the descriptor for GeometryField/RasterField) when assigning a value whose type does not match any accepted form. For raster fields only None/str/dict/raster instances are accepted; for geometry fields None/str/memoryview/GEOSGeometry instances are accepted; everything else is rejected at proxy.py:82.","triggerScenarios":"instance.geom = 12345, instance.geom = ['POINT(0 0)'], or instance.raster = b'\\x89PNG...' (bytes) for a raster field. The descriptor checks isinstance against the field class and the whitelisted scalar types.","commonSituations":"Assigning raw WKB bytes (should be memoryview), a list/dict for a geometry field (raster-only), an int ID, or a shape from another library (shapely) that is not a GEOSGeometry.","solutions":["Convert the value: GEOSGeometry(wkt_or_hexewkb) for geometries, memoryview(wkb) for raw WKB, or GDALRaster(dict) for rasters.","Pass None to clear the field.","For shapely or geojson-py objects, serialize to WKT/GeoJSON first and wrap in GEOSGeometry."],"exampleFix":"// before\nobj.geom = shapely_geom\n// after\nfrom django.contrib.gis.geos import GEOSGeometry\nobj.geom = GEOSGeometry(shapely_geom.wkt)","handlingStrategy":"type-guard","validationCode":"from django.contrib.gis.geos import GEOSGeometry\nfrom django.contrib.gis.gdal import GDALRaster\ndef coerce_geom_value(field, value):\n    if field.geom_type == 'RASTER':\n        if value is None or isinstance(value, (str, dict, GDALRaster)):\n            return value\n    elif value is None or isinstance(value, (str, memoryview, GEOSGeometry)):\n        return value\n    raise TypeError(f'Unsupported value type for {field.attname}: {type(value)}')","typeGuard":"def is_assignable_to_field(field, value):\n    if field.geom_type == 'RASTER':\n        return value is None or isinstance(value, (str, dict, GDALRaster))\n    return value is None or isinstance(value, (str, memoryview, GEOSGeometry))","tryCatchPattern":"try:\n    obj.geom = incoming\nexcept TypeError as e:\n    if 'SpatialProxy' in str(e):\n        obj.geom = GEOSGeometry(incoming.wkt) if hasattr(incoming, 'wkt') else None","preventionTips":["Always wrap foreign geometry types (shapely, geojson) in GEOSGeometry before assignment.","Use memoryview for raw WKB rather than bytes."],"tags":["gis","model-field","type-error","descriptor"],"analyzedSha":"ae25a40be07e8a749edf526df37c93e59d4a22c9","analyzedAt":"2026-08-06T21:46:51.801Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}