serializers.txt 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. ======================
  2. ``GeoJSON`` Serializer
  3. ======================
  4. .. module:: django.contrib.gis.serializers.geojson
  5. :synopsis: Serialization of GeoDjango models in the GeoJSON format.
  6. GeoDjango provides a specific serializer for the `GeoJSON`__ format. See
  7. :doc:`/topics/serialization` for more information on serialization.
  8. The GDAL library is required if any of the serialized geometries need
  9. coordinate transformations (that is if the geometry's spatial reference system
  10. differs from the ``srid`` serializer option).
  11. __ http://geojson.org/
  12. The ``geojson`` serializer is not meant for round-tripping data, as it has no
  13. deserializer equivalent. For example, you cannot use :djadmin:`loaddata` to
  14. reload the output produced by this serializer. If you plan to reload the
  15. outputted data, use the plain :ref:`json serializer <serialization-formats-json>`
  16. instead.
  17. In addition to the options of the ``json`` serializer, the ``geojson``
  18. serializer accepts the following additional option when it is called by
  19. ``serializers.serialize()``:
  20. * ``geometry_field``: A string containing the name of a geometry field to use
  21. for the ``geometry`` key of the GeoJSON feature. This is only needed when you
  22. have a model with more than one geometry field and you don't want to use the
  23. first defined geometry field (by default, the first geometry field is picked).
  24. * ``srid``: The SRID to use for the ``geometry`` content. Defaults to 4326
  25. (WGS 84).
  26. The :ref:`fields <subset-of-fields>` option can be used to limit fields that
  27. will be present in the ``properties`` key, as it works with all other
  28. serializers.
  29. Example::
  30. from django.core.serializers import serialize
  31. from my_app.models import City
  32. serialize('geojson', City.objects.all(),
  33. geometry_field='point',
  34. fields=('name',))
  35. Would output::
  36. {
  37. 'type': 'FeatureCollection',
  38. 'crs': {
  39. 'type': 'name',
  40. 'properties': {'name': 'EPSG:4326'}
  41. },
  42. 'features': [
  43. {
  44. 'type': 'Feature',
  45. 'geometry': {
  46. 'type': 'Point',
  47. 'coordinates': [-87.650175, 41.850385]
  48. },
  49. 'properties': {
  50. 'name': 'Chicago'
  51. }
  52. }
  53. ]
  54. }
  55. When the ``fields`` parameter is not specified, the ``geojson`` serializer adds
  56. a ``pk`` key to the ``properties`` dictionary with the primary key of the
  57. object as the value.
  58. .. versionchanged:: 1.10
  59. The ``pk`` key was added to the ``properties`` dictionary.