【问题标题】:Set serializer geo_field as PointField from another model - Django将序列化程序 geo_field 设置为来自另一个模型的 PointField - Django
【发布时间】: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


    【解决方案1】:

    问题是Article 模型上的point 属性指的是self.locations,即ManyToManyField。你打电话给self.locations.point 好像self.locations 是ForeignKey 这就是你得到错误的原因。

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

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

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

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

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

    【讨论】:

    • 是的,我希望每个Article 有一个或多个locations。我已尝试进行更改,但 GeometryField 不喜欢设置为 many,我收到错误消息:__init__() got an unexpected keyword argument 'many'。没有设置这个我得到错误:'ManyRelatedManager' object is not iterable.
    • 好的,我想我更接近一点。我将.all() 添加到self.locations 中,现在出现Improper geometry input type: <type 'list'> 错误。有什么想法吗?
    • 听起来GeometryField 只设计用于单点,你可能会幸运地将它包装在ListField 中。
    • 更新了我上面的问题。不知道这是否会激发任何想法?
    • @sammy88888888 输出是您所期望的还是您正在寻找不同的东西?
    【解决方案2】:

    好的,解决方案似乎是......不要按照我尝试的方式进行。我通过使用vectorformats.DjangoWStyle(只需搜索谷歌)来处理来自两个模型的序列化geojson,从而解决了大部分问题。此外,试图通过反向关系来实现这将导致一个痛苦的世界。相反,我在Location 模型上添加了ManyToManyField,引用了Article 模型。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-16
      • 1970-01-01
      • 2011-12-22
      • 1970-01-01
      • 2022-01-19
      • 2017-10-20
      相关资源
      最近更新 更多