django-rest-framework-gis 相关领域

2024-03-13

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

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)

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

所以它给了我:

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

代替:

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

合乎逻辑的答案是在序列化器中定义该字段:

geo_field = eventlocation__location

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


我今天早上发现了这一点,它也适用于 DRF-gis:

Django Rest Framework - 在序列化器中获取相关模型字段 https://stackoverflow.com/questions/20633313/django-rest-framework-get-related-model-field-in-serializer

我在 EventLocation 上创建了一个序列化器,并将其定义为 EventSerializer 中的“位置”,并且该点作为 geojson 几何图形输出。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

django-rest-framework-gis 相关领域 的相关文章

随机推荐