serializers.txt 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. .. _ref-geojson-serializer:
  2. ==================
  3. GeoJSON Serializer
  4. ==================
  5. .. versionadded:: 1.8
  6. .. module:: django.contrib.gis.serializers.geojson
  7. :synopsis: Serialization of GeoDjango models in the GeoJSON format.
  8. GeoDjango provides a specific serializer for the `GeoJSON`__ format. The GDAL
  9. library is required for this serializer. See :doc:`/topics/serialization` for
  10. more information on serialization.
  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. }