如何使用api创建PointField?

时间:2016-10-21 22:57:21

标签: django django-rest-framework

我正在使用django-rest-framework作为API,但是当我发布帖子请求时发送给我错误:

{
    "location": [
        "Invalid format: string or unicode input unrecognized as GeoJSON, WKT EWKT or HEXEWKB."
    ]
}

身体要求:

{
    "location":{
        "type":"Point",
        "coordinates":[37.0625,-95.677068]
    }
}

我的模型如下:

class Address(models.Model):
    location = geo.PointField(srid=4326, blank=True)
    objects = geo.GeoManager()

我的序列化程序如下:

class AddressCreateSerializer(serializers.ModelSerializer):
    class Meta:
        model = Address
        fields = ('location')

请帮帮我!

1 个答案:

答案 0 :(得分:5)

我使用django-extra-fields中的PointField来实现此目的。前端开发人员的超级直观和直观(我主要编写移动后端)。有了它,在序列化程序中指定:

from drf_extra_fields.geo_fields import PointField

class AddressCreateSerializer(serializers.ModelSerializer):
    location = PointField()

    class Meta:
        model = Address
        fields = ('location')

请求(猜测你的哪个值是lat,哪个是lon):

{
    "location":{
        "latitude": 37.0625
        "longitude": -95.677068,
    }
}