gpt4 book ai didi

django - GeoDjango 序列化 GeoJSON 跳过 'id' 字段

转载 作者:行者123 更新时间:2023-12-02 07:24:38 26 4
gpt4 key购买 nike

我一直在使用 Geo-Django GeoJSON serializer这样我就可以从 PostGIS 数据库中检索一些对象并将它们显示在 OpenLayers map 上。

我正在通过以下方式获取要显示的对象:

gqs = self.model.objects.filter(point__distance_lte=(pnt, long(dist)))
type(gqs)
<class 'django.contrib.gis.db.models.query.GeoQuerySet'>

并且地理对象按预期包含所有模型字段:

self.model._meta.get_fields()
(<django.db.models.fields.AutoField: id>,
<django.db.models.fields.CharField: name>,
<django.db.models.fields.SlugField: name_slug>,
<django.db.models.fields.CharField: contact>,
<django.db.models.fields.CharField: address>,
<django.db.models.fields.CharField: postcode>,
<django.db.models.fields.EmailField: email>,
<django.db.models.fields.CharField: fax>,
<django.db.models.fields.CharField: tel>,
<django.db.models.fields.CharField: tel1>,
<django.db.models.fields.CharField: tel_fax>,
<django.db.models.fields.URLField: url>,
<django.db.models.fields.CharField: wardlabel>,
<django.db.models.fields.DecimalField: lon>,
<django.db.models.fields.DecimalField: lat>,
<django.db.models.fields.IntegerField: easting>,
<django.db.models.fields.IntegerField: northing>,
<django.db.models.fields.DateField: first_entered>,
<django.db.models.fields.DateField: updated>,
<django.contrib.gis.db.models.fields.PointField: point>)

包括id值...

(Pdb) gqs[0].id
5

然后我以最简单的方式将 GeoQuerySet 传递给 GeoJSON 序列化程序:

gqs_serialized = serialize('geojson', gqs)

并获得输出:

gqs_serialized
u'{"type": "FeatureCollection",
"crs": {
"type": "name",
"properties": {"name": "EPSG:4326"}},
"features": [
{"geometry": {"type": "Point", "coordinates": [-0.19038, 51.490657]}, "type": "Feature",
"properties": {
"tel1": null,
"fax": null,
"tel": null,
"name": "some club",
"url": null,
"wardlabel": "Redcliffe",
"lon": "-0.190380",
"updated": null,
"first_entered": "2013-12-01",
"contact": "some name",
"name_slug": "some club slug",
"postcode": "SW5 0JE",
"easting": 525732,
"address": "Some Address, London",
"lat": "51.490657",
"tel_fax": null,
"email": null,
"northing": 178409}},
{"geometry": {"type": "Point", "coordinates": [-0.137183, 51.495597]}, "type": "Feature",
"properties": { etc...

除了模型中的“id”字段(AutoField)之外,所有内容都存在。我想将 id 值用作网页中的 div id 值,并且当我认为应该已经可用时,我真的不想创建另一个唯一 id(即组合 lon/lat 值)。

缺少的“id”字段发生了什么?

最佳答案

只要输出必须与 specs 兼容, stock 序列化器会忽略不支持的字段。但是,您可以制作自己的序列化程序:

# yourapp/geojson_serializer.py
from django.contrib.gis.serializers import Serializer as GeoJSONSerializer

class Serializer(GeoJSONSerializer):
def get_dump_object(self, obj):
data = super(Serializer, self).get_dump_object(obj)
# Extend to your taste
data.update(id=obj.pk)
return data

settings.py 中启用新的序列化程序:

SERIALIZATION_MODULES = {
"custom_geojson": "yourapp.geojson_serializer",
}

然后在你的代码中使用它:

from django.core import serializers
data = serializers.serialize('custom_geojson', some_model)

关于django - GeoDjango 序列化 GeoJSON 跳过 'id' 字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34556679/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com