Open eruiz67 opened 1 year ago
I got the same issue with : django==4.2
, djangorestframework==3.14.0
and djangorestframework-gis==1.0
.
Note : those versions are not officially supported.
A quick solution is to redefine a new serializer class that correctly instantiate a GEOSGeometry.
Here is such a class :
class InstantiatingGeoFeatureModelSerializer(GeoFeatureModelSerializer):
def to_internal_value(self, data):
result = super().to_internal_value(data)
result[self.Meta.geo_field] = GEOSGeometry(
json.dumps(result[self.Meta.geo_field])
)
return result
A quick solution is to redefine a new serializer class that correctly instantiate a GEOSGeometry.
Here is such a class :
class InstantiatingGeoFeatureModelSerializer(GeoFeatureModelSerializer): def to_internal_value(self, data): result = super().to_internal_value(data) result[self.Meta.geo_field] = GEOSGeometry( json.dumps(result[self.Meta.geo_field]) ) return result
@nemesifier is it in the scope of this library to merge such a fix ? I am willing to make a PR supporting this use-case.
A quick solution is to redefine a new serializer class that correctly instantiate a GEOSGeometry.
Here is such a class :
class InstantiatingGeoFeatureModelSerializer(GeoFeatureModelSerializer): def to_internal_value(self, data): result = super().to_internal_value(data) result[self.Meta.geo_field] = GEOSGeometry( json.dumps(result[self.Meta.geo_field]) ) return result
Ok, the best way to solve this is to implement "validate_GEOSGeometry
there.
For example :
class PolySerializer(GeoFeatureModelSerializer):
class Meta:
model = Poly
geo_field = "geom"
fields = ("id", "name", "created_at")
read_only_fields = ["id"]
def validate_geom(self, value):
value = GEOSGeometry(json.dumps(value))
# The following line is not related to this issue, but it's usefull if your geometry field is a multi-collection
if isinstance(value, Polygon):
value = MultiPolygon(value)
return value
I am working with Django, Django Rest Framework, Django Rest Framework GIS and POSTgis database to create an endpoint that should upload a geografical point and all its related information. I have defined a Model, Serializer and View for that purpose. But when making a request using postman I am getting the following error:
TypeError: Cannot set GeoPoint SpatialProxy (POINT) with value of type: <class 'dict'>
I am currently working with:
Model definition:
Serializer:
View:
This is the image of the POSTman request:
And this is the complete error: