rest Framework не поставляется с полями точек сериализатора по умолчанию для поддержки геометрических полей.
Пожалуйста, проверьте библиотеку ниже для этого.
https://github.com/openwisp/django-rest-framework-gis
или вы можете создать свои собственные поля сериализатора
from rest_framework_gis.serializers import GeometryField
class GeometryPointFieldSerializerFields(GeometryField):
def to_internal_value(self, value):
try:
value = value.split(",")
except:
raise ValidationError(
_("Enter the co-ordinates in (latitude,longitude). Ex-12,13")
)
try:
latitude = float(value[0])
except ValueError:
raise ValidationError(
_("Enter the co-ordinates in (latitude,longitude). Ex-12,13")
)
try:
longitude = float(value[1])
except ValueError:
raise ValidationError(
_("Enter the co-ordinates in (latitude,longitude). Ex-12,13")
)
value = {
"type": "Point",
"coordinates": [longitude, latitude]
}
value = json.dumps(value)
try:
return GEOSGeometry(value)
except (ValueError, GEOSException, OGRException, TypeError):
raise ValidationError(
_('Invalid format: string or unicode input unrecognized as GeoJSON, WKT EWKT or HEXEWKB.'))
def to_representation(self, value):
""" """
value = super(
GeometryPointFieldSerializerFields, self).to_representation(value)
# change to compatible with google map
data = {
"type": "Point",
"coordinates": [
value['coordinates'][1], value['coordinates'][0]
]
}
return data
в сериализаторе можно использовать поля выше
class UserTestSerializer(serializers.ModelSerializer):
location = GeometryPointFieldSerializerFields(source='location.location')
class Meta:
model = User
fields = ('id', 'username', 'location')