django-rest-framework-gis相关领域 [英] django-rest-framework-gis related field

查看:99
本文介绍了django-rest-framework-gis相关领域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个地理模型结构,其中多个事件可以具有相同的位置:

I have a geographical model structure where multiple events can have the same location:

class Event(models.Model):
    name = models.CharField(max_length=128, blank=True, null=True)
    location = models.ForeignKey('MarketLocation', null=True, blank=True)

class EventLocation(models.Model):
    location = models.PointField(srid=4326)

我正在使用django-rest-framework-gis提供的 GeoFeatureModelSerializer 输出单个JSON对象,但 PointField 被呈现为字符串而不是坐标对:

I am using the GeoFeatureModelSerializer provided by django-rest-framework-gis to output a single JSON object but the PointField is being rendered as a string instead of a coordinate pair:

所以它给了我:

"location": "POINT (-1.909 53.7094)"

代替:

  "point": {
        "type": "Point",
        "coordinates": [-123.0208, 44.0464],
   },

逻辑答案是定义领域在序列化器中:

The logical answer would be to define the field in the serializer:

geo_field = eventlocation__location

但是这似乎对输出没有任何影响,这使我认为它可能不起作用,但应该可以。有没有人做过这项工作,如果有的话如何做?

But that doesn't appear to make any difference to the output, which makes me think that it probably doesn't work but it probably should. Has anybody made this work and if so how?

推荐答案

我正在做类似的事情,但是使用MultiPolygon而不是Point。这是我的序列化器:

I'm doing something similar, but with MultiPolygon instead of Point. Here's my serializer:

class AreaSerializer(gis_serializers.GeoFeatureModelSerializer):

    class Meta:
        model = Area
        geo_field = "geom"

也许不需要

以下是我的序列化程序的存储库,如果有帮助的话:

Here's the repo for my Serializer, if that might help:

https://github.com。 com / kelvinn / alerted-us-web / blob / master / apps / alertdb / serializers.py

这篇关于django-rest-framework-gis相关领域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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