GeoDjango序列化GeoJSON跳过' id'领域

时间:2016-01-01 13:50:46

标签: django serialization geojson geodjango

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

我以下列方式获取要显示的对象:

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...

一切都在场,但是&#39; id&#39;来自模型的字段(AutoField)。我想在网页中使用id值作为div id值,并且当我认为应该已经可用时,我真的不想创建另一个唯一ID(即组合lon / lat值)。

失踪的身份发生了什么事?场?

3 个答案:

答案 0 :(得分:3)

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

# 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)

答案 1 :(得分:2)

我也可以使用@jayuu的输入来解决这个问题。对于新读者 对此问题感兴趣,我发布了完整的解决方案:

#myapp/geojson_serializer.py
from django.core import serializers

GeoJSONSerializer = serializers.get_serializer("geojson")

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

然后,要在我的视图中使用它,我刚刚导入了该函数,因为我没有兴趣注册我的序列化程序

#myapp/views.py
from .geojson_serializer import Serializer

def MapDataView(request):
    geojson_serializer = Serializer()
    geojson_serializer.serialize(some_queryset)
    data = geojson_serializer.getvalue()
    return HttpResponse(data, content_type='json')

你有它。

答案 2 :(得分:0)

我解决此问题的一种方法是要求它序列化“ pk”字段,该字段按预期工作。

gqs_serialized = serialize('geojson', gqs, fields=('pk', ))