Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How I can create a PointField using an api?

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."
    ]
}

Body request:

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

My model is as follows:

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')

Help me please!

like image 862
quienesmera Avatar asked Oct 21 '16 22:10

quienesmera


1 Answers

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')

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

{
    "location":{
        "latitude": 37.0625
        "longitude": -95.677068,
    }
}
like image 193
Tomas Walch Avatar answered Oct 17 '22 22:10

Tomas Walch