【发布时间】:2015-05-05 20:43:05
【问题描述】:
我有两个模型,需要通过将 geo_field 属性设置为 point 从 Location 模型将 Article 序列化为 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',)
任何帮助表示赞赏。多年来,我一直在努力寻找解决方案,但收效甚微!
更新!
好的,用这个找到地方。我创建了一个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
]
}
},
【问题讨论】:
标签: django-rest-framework gis geojson geodjango