GeodJango GeoJSON The geometry serializer is always "null"

Welcome friends GeoDjango and GeoJSON serlizer. I followed the official GeoDjango tutorial: https://docs.djangoproject.com/en/1.8/ref/contrib/gis/tutorial/

So, in the end, I have PostgreSQL + PostGIS, full of countries, them name, their code iso3and so on. And especially their geometry in mpolylike MultiPolygon (stored in wkb). I want to get contingents from a database using GeoDjango. I struggle with that.

I can get the properties of one object one by one:

from django.http import HttpResponse
from django.shortcuts import render
from django.core.serializers import serialize
from AppName.models import WorldBorder

[...]

WorldBorder.objects.filter(name='Germany')[0].name           # "Germany"
WorldBorder.objects.filter(name='Germany')[0].iso3           # "DEU"
WorldBorder.objects.filter(name='Germany')[0].mpoly.geojson  # long & correct output

, , . geojson . Django GeoJSON : https://docs.djangoproject.com/en/1.8/ref/contrib/gis/serializers/

:

serialize('geojson',
  WorldBorder.objects.filter(name='Germany'),
  geometry_field='mpoly',
  fields=('name',)
)

:

u'{"type": "FeatureCollection", "crs":{"type": "name", "properties": {"name": "EPSG:4326"}},
    "features": [{"geometry": null,"type": "Feature",
    "properties":{"name": "Germany" }}]}'

, "geometry": null

, , . ? ? ? PostGIS GeoJSON GeoDjango? .

:)

+4
2
+1

- . Django , django.

from django.core.serializers import serialize

'geojson':

serialize('geojson', 
  WorldBorder.objects.filter(name='Germany'),
  geometry_field='geom',
  fields=('id', 'name', 'other_properties_you_want')

! , id .

0

Source: https://habr.com/ru/post/1615937/


All Articles