summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorClaude Paroz <claude@2xlibre.net>2016-02-23 19:57:53 +0100
committerClaude Paroz <claude@2xlibre.net>2016-02-24 16:10:46 +0100
commitc5517b9e7439e542b381a0054bbdab9b5d2bc34b (patch)
tree5fa8c19e7388e4021567b4c9114f1c4a71328563
parent6637cd0ef2fd5f063df82000c18c64c246bb6e1b (diff)
Fixed #26266 -- Output the primary key in the GeoJSON serializer properties
Thanks Tim Graham for the review.
-rw-r--r--django/contrib/gis/serializers/geojson.py3
-rw-r--r--docs/ref/contrib/gis/serializers.txt8
-rw-r--r--docs/releases/1.10.txt4
-rw-r--r--tests/gis_tests/geoapp/test_serializers.py3
4 files changed, 18 insertions, 0 deletions
diff --git a/django/contrib/gis/serializers/geojson.py b/django/contrib/gis/serializers/geojson.py
index b26ec2e809..68778c8753 100644
--- a/django/contrib/gis/serializers/geojson.py
+++ b/django/contrib/gis/serializers/geojson.py
@@ -47,6 +47,9 @@ class Serializer(JSONSerializer):
"type": "Feature",
"properties": self._current,
}
+ if ((self.selected_fields is None or 'pk' in self.selected_fields) and
+ 'pk' not in data["properties"]):
+ data["properties"]["pk"] = obj._meta.pk.value_to_string(obj)
if self._geometry:
if self._geometry.srid != self.srid:
# If needed, transform the geometry in the srid of the global geojson srid
diff --git a/docs/ref/contrib/gis/serializers.txt b/docs/ref/contrib/gis/serializers.txt
index 1a9fb8c8cb..7d2b80a217 100644
--- a/docs/ref/contrib/gis/serializers.txt
+++ b/docs/ref/contrib/gis/serializers.txt
@@ -71,3 +71,11 @@ Would output::
}
]
}
+
+When the ``fields`` parameter is not specified, the ``geojson`` serializer adds
+a ``pk`` key to the ``properties`` dictionary with the primary key of the
+object as the value.
+
+.. versionchanged:: 1.10
+
+ The ``pk`` key was added to the ``properties`` dictionary.
diff --git a/docs/releases/1.10.txt b/docs/releases/1.10.txt
index d4428ebfda..a5e68759da 100644
--- a/docs/releases/1.10.txt
+++ b/docs/releases/1.10.txt
@@ -131,6 +131,10 @@ Minor features
:attr:`MultiLineString.closed
<django.contrib.gis.geos.MultiLineString.closed>` properties.
+* The :doc:`GeoJSON serializer </ref/contrib/gis/serializers>` now outputs the
+ primary key of objects in the ``properties`` dictionary if specific fields
+ aren't specified.
+
:mod:`django.contrib.messages`
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
diff --git a/tests/gis_tests/geoapp/test_serializers.py b/tests/gis_tests/geoapp/test_serializers.py
index de104b5cda..69ec73db74 100644
--- a/tests/gis_tests/geoapp/test_serializers.py
+++ b/tests/gis_tests/geoapp/test_serializers.py
@@ -33,6 +33,8 @@ class GeoJSONSerializerTests(TestCase):
self.assertEqual(len(geodata['features']), len(City.objects.all()))
self.assertEqual(geodata['features'][0]['geometry']['type'], 'Point')
self.assertEqual(geodata['features'][0]['properties']['name'], 'Chicago')
+ first_city = City.objects.all().order_by('name').first()
+ self.assertEqual(geodata['features'][0]['properties']['pk'], str(first_city.pk))
def test_geometry_field_option(self):
"""
@@ -76,6 +78,7 @@ class GeoJSONSerializerTests(TestCase):
geodata = json.loads(geojson)
self.assertIn('county', geodata['features'][0]['properties'])
self.assertNotIn('founded', geodata['features'][0]['properties'])
+ self.assertNotIn('pk', geodata['features'][0]['properties'])
def test_srid_option(self):
geojson = serializers.serialize('geojson', City.objects.all().order_by('name'), srid=2847)