You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I currently observe the behavior that a ModelSerializer does not validate if the geometry field is of the right geometry type. Hence, the serializer does not throw a validation error when it receives data of an unexpected geometry type but later runs into a TypeError that is thrown by Django when the data is saved to the database.
File "/usr/local/lib/python3.10/site-packages/django/db/models/manager.py", line 87, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/usr/local/lib/python3.10/site-packages/django/db/models/query.py", line 656, in create
obj = self.model(**kwargs)
File "/usr/local/lib/python3.10/site-packages/django/db/models/base.py", line 546, in __init__
_setattr(self, field.attname, val)
File "/usr/local/lib/python3.10/site-packages/django/contrib/gis/db/models/proxy.py", line 76, in __set__
raise TypeError(
TypeError: Cannot set Plot SpatialProxy (POLYGON) with value of type: <class 'django.contrib.gis.geos.point.Point'>
However, for my opinion a ValidationError would be more appropriated to be raised.
Problem
I think the problem comes done to the field_mapping in the apps.py module which maps the GeoDjango fields to a generic GeometryField.
My idea would be to append a GeometryFieldValidator to validators list of the GeometryField:
classGeometryValidator:
def__init__(self, geom_type):
self.geom_type=geom_typedef__call__(self, value):
ifvalue.geom_type!=self.geom_type:
raiseserializers.ValidationError("Wrong geometry type provided.", code="invalid")
classGeometryField(Field):
""" A field to handle GeoDjango Geometry fields """type_name='GeometryField'def__init__(
self, precision=None, remove_duplicates=False, auto_bbox=False, geom_type=None, **kwargs
):
""" :param auto_bbox: Whether the GeoJSON object should include a bounding box """self.precision=precisionself.auto_bbox=auto_bboxself.remove_dupes=remove_duplicatessuper().__init__(**kwargs)
self.style.setdefault('base_template', 'textarea.html')
ifgeom_type:
self.validators.append(GeometryValidator(geom_type))
I have not thought about the specifics of the implementation. However, I would be open to work on a PR if the described behavior of raising a ValidationError is of interest for the community. Or do you think one should simply solve this by adding the described GeometryValidator manually to each ModelSerializer which contains a geometry field, i.e.:
I currently observe the behavior that a
ModelSerializer
does not validate if the geometry field is of the right geometry type. Hence, the serializer does not throw a validation error when it receives data of an unexpected geometry type but later runs into aTypeError
that is thrown by Django when the data is saved to the database.Example
If the
/polygons
end point would receive a POST/PUT request with the following bodythe following error would be raised by Django:
However, for my opinion a
ValidationError
would be more appropriated to be raised.Problem
I think the problem comes done to the
field_mapping
in theapps.py
module which maps the GeoDjango fields to a genericGeometryField
.django-rest-framework-gis/rest_framework_gis/apps.py
Lines 24 to 35 in 4f244d5
Solution
My idea would be to append a
GeometryFieldValidator
tovalidators
list of theGeometryField
:I have not thought about the specifics of the implementation. However, I would be open to work on a PR if the described behavior of raising a
ValidationError
is of interest for the community. Or do you think one should simply solve this by adding the describedGeometryValidator
manually to eachModelSerializer
which contains a geometry field, i.e.:Happy to get your feedback on this :).
The text was updated successfully, but these errors were encountered: