9

I'm using Django 1.8, with GeoDjango and PostGIS. I am using HttpResponse to return some GeoJSON:

from django.http import HttpResponse, JsonResponse
code = request.GET.get('q', '')
results = PCT.objects.filter(Q(code__startswith=code) |
                                 Q(name__icontains=code))
results = results.filter(org_type='CCG')
for result in results:
    print result.code
geo_field = 'boundary'
fields = ('name', 'code', 'ons_code', 'org_type', 'boundary', )
return HttpResponse(serialize('geojson', results,
                    geometry_field=geo_field, fields=fields),
                    content_type='application/json')

In the console this prints a code field just fine:

99N

But the GeoJSON returned does not have a properties.code field. It has a properties.name, properties.org_type and properties.ons_code field though.

enter image description here

Why is this? Is code a reserved name perhaps? If so, how can I fix this?

Richard
  • 62,943
  • 126
  • 334
  • 542
  • May be 'code' doesn't exist when it's None or has no value , I see the printed value on console '99N' appears once , does results list have only one object ? Can we see the whole response ? – Amr Abdelaziz Jun 17 '15 at 12:36
  • 1
    I believe this is *because* ``code`` is not a valid part or proeprty of the [GeoJSON](http://geojson.org/geojson-spec.html#feature-objects) specification and is not valid to put into a Feature object in serialized form. ``django-geojson`` is doing the *right* thing here AFAIK. – James Mills Jun 18 '15 at 11:49
  • @Richard: I've done my best to answer your question and will happily refine the answer if it didn't help. Can you please show what your model is and the output from serializing the object with no filters was? – Peter Brittain Jun 23 '15 at 11:44
  • @Richard Is your 'code' attribute a ForeignKey? – jcs Jun 30 '15 at 10:19
  • 1
    Possible duplicate of [GeoDjango serialize GeoJSON skipping 'id' field](http://stackoverflow.com/questions/34556679/geodjango-serialize-geojson-skipping-id-field) – user528025 Mar 30 '16 at 23:41
  • Actually, I'd argue it was the other way around given that this was raised 6 months earlier. – Peter Brittain Oct 19 '17 at 12:02

1 Answers1

5

I've had a quick look at the GeoJSON spec and it would appear that it only goes so far as to say that the properties field is a JSON object in its own right, so I think you're in the letter of the current specification if you want it in that part of the JSON dump. That said, this spec is still in draft form and so subject to change (and may yet place extra constraints on this field). Assuming that you can live with that, we can continue...

The code that handles this is in the geojson serializer. This currently will only create data for the geometry, type and properties fields in get_dump_object(). But you'll note that the properties field renders whatever is in self._current. That field is actually built up (by the parent classes' methods) as the serializer iterates over the rest of the fields in the object.

By the time get_dump_object() is called, self._current should contain all the other serializable fields in the object. As you can see in the base serializer class, fields will only be serialized if they are constructed with serialize=True and the field is in the list of specified fields you passed in to serialize() (or you didn't specify a filter so you're going to get everything). I would therefore guess that your code field has been declared as not serializable, or it has an unexpected internal name that doesn't match your filter.

To try to fix it, I would have a look at your declaration of the code field in your Model for a bad serialize parameter, then just try serializing without any field list at all. Hopefully one of those gets your missing field into the JSON.

Peter Brittain
  • 13,489
  • 3
  • 41
  • 57