2

您好,我正在尝试使用 Djano Rest Framework Gis 生成 GEOJSON 输出:

这是我的 Models.py

from django.contrib.gis.db import models
from django_autoslug.fields import AutoSlugField
from django_extensions.db.fields import UUIDField

class Location(models.Model):
    name = models.CharField(max_length=255)
    slug = AutoSlugField(populate_from='name', max_length=255)
    uuid = UUIDField(auto=True)
    point = models.PointField(help_text="Represented as (longitude, latitude)")
    objects = models.GeoManager()

和 serializers.py

from django.forms import widgets
from rest_framework_gis import serializers
from locateMe.models import Location

class LocationSerializer(serializers.GeoFeatureModelSerializer):
    class Meta:
        model = Location
        geo_field = "point"
        id_field = "slug"
        fields = ('slug', 'uuid')

我得到这个错误:

  argument of type 'NoneType' is not iterable
  Exception Location: ../rest_framework_gis/serializers.py in from_native, line 115

所以我看了一下 /rest_framework_gis/serializers.py

def from_native(self, data, files):
    """
    Override the parent method to first remove the GeoJSON formatting
    """
    if 'features' in data:
        _unformatted_data = []
        features = data['features']
        for feature in features:
            _dict = feature["properties"]
            geom = { self.opts.geo_field: feature["geometry"] }
            _dict.update(geom)
            _unformatted_data.append(_dict)
    elif 'properties' in data:
        _dict = data["properties"]
        geom = { self.opts.geo_field: data["geometry"] }
        _dict.update(geom)
        _unformatted_data = _dict
    else:
        _unformatted_data = data

    data = _unformatted_data

    instance = super(GeoFeatureModelSerializer, self).from_native(data, files)
    if not self._errors:
        return self.full_clean(instance)

这可能很明显,但我看不出有什么问题

4

0 回答 0