GeoDjango序列化GeoJSON跳过'id'字段 [英] GeoDjango serialize GeoJSON skipping 'id' field
问题描述
我一直在使用Geo-Django GeoJSON serializer ,以便我可以从PostGIS数据库检索一些对象并将其显示在OpenLayers地图上。
I've been using the Geo-Django GeoJSON serializer so that I can retrieve some objects from a PostGIS database and display them on an OpenLayers map.
我正在获取在以下方式:
I'm obtaining the objects for display in the following way:
gqs = self.model.objects.filter(point__distance_lte=(pnt, long(dist)))
type(gqs)
<class 'django.contrib.gis.db.models.query.GeoQuerySet'>
,地理对象包括所有模型字段如下:
and the Geo-Objects include all the model fields as expected:
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序列化程序:
I then pass the GeoQuerySet to the GeoJSON serializer in the simplest possible way:
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值)。
Everything is present but the 'id' field (AutoField) from the model. I want to use the id values as div id values in a webpage and I don't really want to have to create another unique id (ie. combining lon/lat values) when I think one should already be available.
失踪的id字段发生了什么?
What happened to the missing 'id' field?
推荐答案
输出必须与规格兼容,库存序列化程序将忽略不受支持的字段。但是,您可以制作自己的序列化程序:
As far as the output must be compatible with the specs, the stock serializer omits unsupported fields. However, you could craft your own serializer:
# 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
中启用新的序列化程序:
Enable your new serializer in settings.py
:
SERIALIZATION_MODULES = {
"custom_geojson": "yourapp.geojson_serializer",
}
然后在代码中使用它:
from django.core import serializers
data = serializers.serialize('custom_geojson', some_model)
这篇关于GeoDjango序列化GeoJSON跳过'id'字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!