Python 在GeoDjango中添加属性
我不断地发现这个错误:Python 在GeoDjango中添加属性,python,json,serialization,geojson,geodjango,Python,Json,Serialization,Geojson,Geodjango,我不断地发现这个错误: TypeError at / <Section: BILAY - 001> is not JSON serializable 在my models.py中: class ButuanMaps(gismodel.Model): class Meta: verbose_name = u'Butuan Map' verbose_name_plural = u'Butuan Maps' clandpin = gism
TypeError at /
<Section: BILAY - 001> is not JSON serializable
在my models.py中:
class ButuanMaps(gismodel.Model):
class Meta:
verbose_name = u'Butuan Map'
verbose_name_plural = u'Butuan Maps'
clandpin = gismodel.CharField("Land PIN", max_length=50, null=True, blank=True)
ssectionid = gismodel.ForeignKey(Section)
narea = gismodel.DecimalField(max_digits=20, decimal_places=6)
#ssectionid_id = gismodel.IntegerField()
geom = gismodel.MultiPolygonField("Geom ID", srid=32651, null=True, blank=True)
objects = gismodel.GeoManager()
def __unicode__(self):
return self.clandpin
当我在属性上添加narea
和ssectionid
时,它返回一个错误。为什么?在这方面:
djf = Django.Django(geodjango='geom', properties= ['id','clandpin','ssectionid','narea'])
我对Django.Django-GeoJSON.GeoJSON()方法也有同样的问题 错误很明显,它告诉您字段“不可JSON序列化”
- 第一个是因为它是十进制的,所以我猜它不明白你的小数点,或者它应该是昏迷或点
- 第二个是因为它是外键,处理得不好
prop = MyModel._meta.get_all_field_names()
2-删除'ssectionid'属性
prop.remove("ssectionid")
3解码
djf = Django.Django(geodjango='geom', properties = prop)
decod = djf.decode(obj)
4-Get foreignkey值
sectionlist = MyModel.objects.filter().values('ssectionid__id')
5-对于'decod'的每个元素,添加属性'ssectionid'
for el in decod:
el.to_dict()["properties"].update({"ssectionid": sectionlist.get(id=el.properties['id'])["ssectionid__id"]})
6-编码
geojson = geoj.encode(decod)
我不是说这是一个好答案,而是说它是有效的
编辑
这个包裹就是你要找的
geojson = geoj.encode(decod)