1

我正在使用 rest_framework_gis 编写一个 Web 应用程序。我的一个模型有 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'

然后我用 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', )

最后是我的观点:

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

我的问题是我应该在位置字段中输入什么样的数据?我知道它应该是 JSON 格式。但我到底要写什么呢?如果有人可以举个例子,那将非常有帮助

4

1 回答 1

0

哦,男孩,这很尴尬。在django-rest-framework-gis的GitHub 存储库上找到了答案正确的例子是这样的:

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

希望,这对正在搜索的人有所帮助

于 2020-04-17T16:06:48.593 回答