将序列化器 geo_field 设置为另一个模型中的 PointField - Django

2023-12-02

我有两个模型,需要序列化Article作为 Geojson 通过设置geo_field属性为point来自Location模型。按照给出的解决方案后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
                ]
            }
        },

问题是你的point财产在Article模型指的是self.locations这是一个ManyToManyField。你正在呼唤self.locations.point好像self.locations is a ForeignKey这就是你收到错误的原因。

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

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

  1. 你确实想要拥有locations be a ForeignKey,这意味着你的point属性是正确的。
  2. 您实际上期望有多个位置Article, 和你的point属性实际上应该返回点列表。

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

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

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

将序列化器 geo_field 设置为另一个模型中的 PointField - Django 的相关文章

随机推荐