将串行器geo_field设置为另一个模型的PointField - Django

时间:2015-05-05 20:43:06

标签: django-rest-framework gis geojson geodjango

我有两个模型,需要通过将Article属性geo_field设置为point模型,将Location序列化为Geojson。在给出here给出的解决方案后,我收到错误:

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',)

任何帮助表示赞赏。我一直试图找到一个解决这个问题的方法,但收效甚微!

UPDATE!

好的,到处找个地方。我创建了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"
            }
        }
    ]
}

UPDATE!

我要求的格式是:

{
    "type": "FeatureCollection",
    "features": [
        {
            "type": "Feature",
            "properties": {
                "time": "2013-01-22 08:42:26+01"
            },
            "geometry": {
                "type": "Point",
                "coordinates": [
                    7.582512743,
                    51.933292258,
                    1
                ]
            }
        },

2 个答案:

答案 0 :(得分:0)

问题是point模型上的Article属性是指self.locations ManyToManyField。您正在呼叫self.locations.point,好像self.locationsForeignKey,这就是您收到错误的原因。

  

' ManyRelatedManager'对象没有属性' point'。

表示self.locations正在返回ManyRelatedManager,这对ManyToManyField有意义。由于您只需要一个对象,这意味着您需要进行两次更改之一

  1. 您真的希望locations成为ForeignKey,这意味着您的point属性是正确的。
  2. 您实际上期待Article的多个位置,而您的point属性实际上应该返回一个点列表。

    return [location.point for location in self.locations]
    
  3. 如果您实际上在寻找多个位置,那么您还需要在序列化程序的many=True上设置GeometryField

答案 1 :(得分:0)

好的,解决方案似乎是......不要像我尝试的那样去做。通过使用vectorformats.DjangoWStyle(只搜索谷歌)处理来自两个模型的searializing geojson,我解决了大部分问题。而且,尝试反向关系以实现这将导致一个痛苦的世界。相反,我在ManyToManyField模型上添加了Location,引用了Article模型。