Django rest framework 将序列化器geo_字段设置为来自另一个模型-Django的PointField

Django rest framework 将序列化器geo_字段设置为来自另一个模型-Django的PointField,django-rest-framework,gis,geojson,geodjango,Django Rest Framework,Gis,Geojson,Geodjango,我有两个模型,需要通过从位置模型将geo_字段属性设置为point,将文章序列化为Geojson。遵循给定的解决方案后,我得到错误: Got AttributeError when attempting to get a value for field `point` on serializer `ArticleSerializer`. The serializer field might be named incorrectly and not match any attribute or k

我有两个模型,需要通过从位置模型将geo_字段属性设置为point,将文章序列化为Geojson。遵循给定的解决方案后,我得到错误:

Got AttributeError when attempting to get a value for field `point` on serializer `ArticleSerializer`.
The serializer field might be named incorrectly and not match any attribute or key on the `Article` instance.
Original exception text was: 'ManyRelatedManager' object has no attribute 'point'.
以下是我的模型:

class Location(models.Model):
    city = models.CharField(max_length=200)
    point = models.PointField(srid=4326)
    objects = models.GeoManager()

class Article(models.Model):
    locations = models.ManyToManyField(Location)
    article_title = models.CharField(max_length=200)

    @property
    def point(self):
    return self.locations.point
和我的序列化程序:

class ArticleSerializer(GeoFeatureModelSerializer):
point = GeometryField()

class Meta:
    model = Article
    geo_field = "point"
    id_field = False
    fields = ('pub_date', 'point',)
谢谢你的帮助。多年来,我一直在试图找到解决这个问题的办法,但收效甚微

更新

好吧,这件事有进展了。我创建了一个LocationSerializer,并在ArticleSerializer中引用了它。这些现在看起来像这样:

class LocationSerializer(serializers.ModelSerializer):
    class Meta:
        model = Location
        geo_field = "point"
        id_field = False
        fields = ('point',)

class ArticleSerializer(GeoFeatureModelSerializer):
    locations = LocationSerializer(many=True)       
    class Meta:
        model = Article
        geo_field = "locations"
        id_field = False
        fields = ('pub_date', 'locations',)
输出更接近我想要的…但在构造上仍然有点模糊:

{  
    "type":"FeatureCollection",
    "features":[  
        {  
            "type":"Feature",
            "geometry":[  
                {  
                    "point":{  
                        "type":"Point",
                        "coordinates":[  
                            -2.200337956118645,
                            53.48316423741371
                        ]
                    }
                },
                {  
                    "point":{  
                        "type":"Point",
                        "coordinates":[  
                            0.041198730463564,
                            51.51002453017606
                        ]
                    }
                }
            ],
            "properties":{  
                "pub_date":"2015-04-06T20:38:59Z"
            }
        }
    ]
}
更新

我要求的格式是:

{
    "type": "FeatureCollection",
    "features": [
        {
            "type": "Feature",
            "properties": {
                "time": "2013-01-22 08:42:26+01"
            },
            "geometry": {
                "type": "Point",
                "coordinates": [
                    7.582512743,
                    51.933292258,
                    1
                ]
            }
        },
问题是,文章模型上的point属性引用了self.locations,它是一个ManyToManyField。您正在调用self.locations.point,就好像self.locations是一个外键一样,这就是为什么会出现错误

“ManyRelatedManager”对象没有属性“point”

这意味着self.locations将返回一个ManyRelatedManager,这对于ManyToManyField是有意义的。由于您只需要一个对象,这意味着您需要进行两个更改之一

您确实希望将位置设置为ForeignKey,这意味着您的point属性是正确的。 实际上,您希望一篇文章有多个位置,而point属性实际上应该返回一个点列表

return [location.point for location in self.locations]

如果您实际上要查找多个位置,您还需要在序列化程序的GeometryField上设置many=True。

好的,解决方案似乎是……不要像我尝试的那样。我已经通过使用vectorformats.DjangoWStyle解决了大部分问题,只需搜索google就可以处理来自两个模型的searializing geojson。此外,试图建立一种相反的关系来实现这一目标将导致一个痛苦的世界。相反,我添加了位置模型上的ManyToManyField,引用了文章模型。

是的,我希望每篇文章都有一个或多个位置。我已尝试进行更改,但GeometryField不喜欢设置为“多”,我得到了一个错误:\uuu init\uuu获得了一个意外的关键字参数“多”。如果不设置此选项,我会得到错误:“ManyRelatedManager”对象是不可编辑的。好的,我想我更接近了。我将.all添加到self.locations,现在得到不正确的几何输入类型:error。有什么想法吗?听起来GeometryField只设计用于处理单个点,您可能会幸运地将其包装在列表字段中。更新了我上面的问题。不知道这是否会激发任何想法?@sammy8888888是您期望的输出,还是您正在寻找不同的输出?