简体   繁体   English

如何使用区域字段更新 GeoDjango model

[英]How to update GeoDjango model with area fields

Here the model:这里是 model:

from django.contrib.gis.db import models    
class City(models.Model):
    mpoly = models.MultiPolygonField()
    area = models.IntegerField(null=True)

I want to update all records at once to set the area.我想一次更新所有记录来设置区域。 I've tried:我试过了:

from django.contrib.gis.db.models.functions import Area, Transform

City.objects.all().update(area=Area(Transform("mpoly", 2154)))

Which fails with "DataError: value too long for type character varying(8)"...失败并显示“DataError:类型字符变化的值太长(8)”......

Do you have an elegant one liner to do it?你有优雅的单衬垫吗?

As a matter of fact, my problem was that I try to put a large number in a char field (of 8 characters).事实上,我的问题是我试图在 char 字段(8 个字符)中放入大量数字。 The provided example and the error are unrelated.提供的示例和错误无关。

To make it work with the provided model:要使其与提供的 model 一起使用:

from django.contrib.gis.db.models.functions import Area, Transform
from django.db.models import IntegerField
from django.db.models.functions import Cast

City.objects.all().update(area=Cast(Area(Transform("mpoly", 2154)), IntegerField()))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM