serializers.txt 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. .. versionchanged:: 1.9
  12. The GeoJSON serializer no longer needs GDAL if all geometries are in the
  13. same coordinate system as the ``srid`` serializer option.
  14. __ http://geojson.org/
  15. The ``geojson`` serializer is not meant for round-tripping data, as it has no
  16. deserializer equivalent. For example, you cannot use :djadmin:`loaddata` to
  17. reload the output produced by this serializer. If you plan to reload the
  18. outputted data, use the plain :ref:`json serializer <serialization-formats-json>`
  19. instead.
  20. In addition to the options of the ``json`` serializer, the ``geojson``
  21. serializer accepts the following additional option when it is called by
  22. ``serializers.serialize()``:
  23. * ``geometry_field``: A string containing the name of a geometry field to use
  24. for the ``geometry`` key of the GeoJSON feature. This is only needed when you
  25. have a model with more than one geometry field and you don't want to use the
  26. first defined geometry field (by default, the first geometry field is picked).
  27. * ``srid``: The SRID to use for the ``geometry`` content. Defaults to 4326
  28. (WGS 84).
  29. The :ref:`fields <subset-of-fields>` option can be used to limit fields that
  30. will be present in the ``properties`` key, as it works with all other
  31. serializers.
  32. Example::
  33. from django.core.serializers import serialize
  34. from my_app.models import City
  35. serialize('geojson', City.objects.all(),
  36. geometry_field='point',
  37. fields=('name',))
  38. Would output::
  39. {
  40. 'type': 'FeatureCollection',
  41. 'crs': {
  42. 'type': 'name',
  43. 'properties': {'name': 'EPSG:4326'}
  44. },
  45. 'features': [
  46. {
  47. 'type': 'Feature',
  48. 'geometry': {
  49. 'type': 'Point',
  50. 'coordinates': [-87.650175, 41.850385]
  51. },
  52. 'properties': {
  53. 'name': 'Chicago'
  54. }
  55. }
  56. ]
  57. }