问题
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>)
Including the id value...
(Pdb) gqs[0].id
5
I then pass the GeoQuerySet to the GeoJSON serializer in the simplest possible way:
gqs_serialized = serialize('geojson', gqs)
and get output:
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...
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.
What happened to the missing 'id' field?
回答1:
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
Enable your new serializer in settings.py
:
SERIALIZATION_MODULES = {
"custom_geojson": "yourapp.geojson_serializer",
}
And then use it in your code:
from django.core import serializers
data = serializers.serialize('custom_geojson', some_model)
回答2:
I was able to solve this as well by using the input from @jayuu . For new readers interested in this issue I post the complete solution:
#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
Then, to use it in my views I just imported the function because I had no interest in registering my serializer
#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')
And there you have it.
回答3:
One way I solved this was asking it to serialize the "pk" field, which worked as expected.
gqs_serialized = serialize('geojson', gqs, fields=('pk', ))
来源:https://stackoverflow.com/questions/34556679/geodjango-serialize-geojson-skipping-id-field