如何使用api创建PointField? [英] How I can create a PointField using an api?

查看:104
本文介绍了如何使用api创建PointField?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为API使用django-rest-framework,但是当我发出发布请求时,会向我发送此错误:

I am using django-rest-framework for the API, but when I make a post request sends me this error:

{
    "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()

我的序列化器是一个跟随者:

My serializer is a follow:

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

请帮助我!

推荐答案

我使用 PointField django-extra-fields 的$ c> 。对于前端开发人员来说超级好用且直观(我主要写移动后端)。这样,在序列化器中指定:

I use PointField from django-extra-fields for this. Supernice and intuitive for frontend developers (I write mobile backends mostly). With that, in your serializer specify:

from drf_extra_fields.geo_fields import PointField

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

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

请求(猜测您的哪个值是经纬度,哪个值是lon):

Request (taking a guess as to which of your values is lat and which is lon):

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

这篇关于如何使用api创建PointField?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆