Data for PointField in rest_framework_gis

842 views Asked by At

I am writing a web application using rest_framework_gis. One of my models has PointField

from django.contrib.gis.db import models
from django.contrib.auth import get_user_model

User = get_user_model()


class Avatar(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE, related_name='avatar')
    location = models.PointField(srid=4326)

    def __str__(self):
        return self.user.username + '\'s avatar'

Then I serialize it with GeoFeatureModelSerializer

from rest_framework_gis.serializers import GeoFeatureModelSerializer
from .models import Avatar


class AvatarSerializer(GeoFeatureModelSerializer):

    class Meta:
        model = Avatar
        geo_field = 'location'
        auto_bbox = True
        fields = ('location', )

And finally my views:

from .models import Avatar
from .serializers import AvatarSerializer
from rest_framework_gis.filters import DistanceToPointFilter


class AvatarViewSet(viewsets.ModelViewSet):

    queryset = Avatar.objects.all()
    serializer_class = AvatarSerializer
    distance_filter_field = 'geometry'
    filter_backends = (DistanceToPointFilter, )
    bbox_filter_include_overlapping = True

My question is what kind of data do I input into location field? I know it should be in JSON format. But what exactly do I write? If anyone could give an example it would be really helpful

1

There are 1 answers

0
Bektur Soltobaev On

Oh boy, this is embarrassing. Found the answer on GitHub repository of django-rest-framework-gis The correct example is like this:

    {
    "id": 1,
    "type": "Feature",
    "geometry": {
        "type": "Point",
        "coordinates": [-123.0208, 44.0464],
    },
    "properties": {
        "address": "742 Evergreen Terrace",
        "city":  "Springfield",
        "state": "Oregon"
    }
}

Hope, this helps to those who were searching