2

我正在使用 Django DRF 和 GeoDjango 构建一个简单的模型,如下所示。

class Company(models.Model):
    name = models.CharField(max_length=200, default='Company', null=True)

    def __unicode__(self):
        return self.name


class Shop(models.Model):
    name = models.CharField(max_length=200, default="bla")
    address = models.CharField(max_length=300, default='blabla')
    location = models.PointField(null=True, blank=True, geography=True)
    company = models.ForeignKey(
        Company, on_delete=models.CASCADE, null=True)

这是它的serializer.py

class ShopSerializer(serializers.ModelSerializer):

    distance = serializers.DecimalField(
        source='distance.km', max_digits=10, decimal_places=2, required=False, read_only=True)


    class Meta:
        model = Shop
        fields = ['id', 'name', 'address', 'location', 'distance']
        # read_only_fields = ['distance']


class CompanySerializer(serializers.ModelSerializer):
    shop_set = ShopSerializer(many=True)

    class Meta:
        model = Company
        fields = ['id', 'name', 'shop_set']

    def create(self, validated_data):
        shop_validated_data = validated_data.pop('shop_set')
        company = Company.objects.create(**validated_data)
        shop_set_serializer = self.fields['shop_set']
        for each in shop_validated_data:
            each['company'] = company
        shops = shop_set_serializer.create(shop_validated_data)
        return company

一切正常,直到我添加rest_framework_gis我的 settings.py 文件或在我的商店序列化程序中添加以下行

serialize('geojson', Shop.objects.all(),
               geometry_field='location', fields=('name', 'address'))

在这两种情况下都会出现 GDALException OGR 失败。我检查了我的 GDAL 和 Python 版本。两者都是 64 位的。python 和 GDAL 都运行良好。

我在这里基本上需要做的是将我的 POINT 字段转换为 json lat long 响应,现在响应就是这样(如果我不包括导致错误的行)。

{
        "id": 1,
        "name": "Cosmetica",
        "address": "somwhere",
        "location": "SRID=4326;POINT (24.896 67.182)"
    }

请帮忙。

4

0 回答 0