python - 将 OFTInteger 转换为 Decimal 时 Django 错误

标签 python django python-2.7 postgresql-9.1 geos

我正在使用 PostgreSQL 9.1、Django 1.3.1、psycopg2 2.4.5 以及使用库 GEOS 3.3.0、PostGIS 1.5.4 和 ProJ 4.7.0 构建一个包含地理数据的数据库。

我有一个命令将形状文件加载到数据库中,并进行了一些优化以使形状更简单。

为了保持模型简单,我将只放置有问题的字段:

class Circonscription(models.Model):
    shape = models.GeometryField()
    simple_shape = models.GeometryField()
    centroid = models.PointField(null=True)

当我想从形状文件创建一个时,我这样做:

Circonscription.objects.create(
            id_cep=feature['id_cep'],
            co_cep=feature['co_cep'],
            nm_cep=feature['nm_cep'],
            nmtri_cep=feature['nmtri_cep'],
            dh_maj=feature['dh_maj'],
            shape=geometry.wkt,
            simple_shape=simple_geometry.wkt,
            centroid=geometry.geos.centroid)

现在,当我运行命令时,它失败并出现以下错误:

File "/home/pg/Projets/python/myproject/circonscriptions/management/commands/loadshapefiles.py", line 174, in add_boundaries_for_layer
centroid=geometry.geos.centroid)
File "/home/pg/.virtualenvs/myproject/local/lib/python2.7/site-packages/django/db/models/manager.py", line 138, in create
    return self.get_query_set().create(**kwargs)
  File "/home/pg/.virtualenvs/myproject/local/lib/python2.7/site-packages/django/db/models/query.py", line 360, in create
    obj.save(force_insert=True, using=self.db)
  File "/home/pg/.virtualenvs/myproject/local/lib/python2.7/site-packages/django/db/models/base.py", line 460, in save
    self.save_base(using=using, force_insert=force_insert, force_update=force_update)
  File "/home/pg/.virtualenvs/myproject/local/lib/python2.7/site-packages/django/db/models/base.py", line 543, in save_base
    for f in meta.local_fields if not isinstance(f, AutoField)]
  File "/home/pg/.virtualenvs/myproject/local/lib/python2.7/site-packages/django/db/models/fields/subclassing.py", line 28, in inner
    return func(*args, **kwargs)
  File "/home/pg/.virtualenvs/myproject/local/lib/python2.7/site-packages/django/db/models/fields/__init__.py", line 786, in get_db_prep_save
    return connection.ops.value_to_db_decimal(self.to_python(value),
  File "/home/pg/.virtualenvs/myproject/local/lib/python2.7/site-packages/django/db/models/fields/__init__.py", line 761, in to_python
    return decimal.Decimal(value)
  File "/usr/lib/python2.7/decimal.py", line 658, in __new__
    raise TypeError("Cannot convert %r to Decimal" % value)
TypeError: Cannot convert <django.contrib.gis.gdal.field.OFTInteger object at 0x2458b50> to Decimal

有人知道这件事吗?

最佳答案

这与值的编码有关,然后另一个错误是关于 this problem最终解决了这个问题。 在将值传递给模型进行保存之前,请务必将它们转换为正确的编码 (utf-8)。

关于python - 将 OFTInteger 转换为 Decimal 时 Django 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11478714/

相关文章:

c++ - 效益成本分析库

python - 将 Python 嵌入到 C 中——导入模块

jquery - Django django-autocomplete-light 搜索不完整

Vim 的 Python 和 Django 插件

python - 计算字典中键值对的数量

python - pyspark: ImportError: 没有名为 numpy 的模块

python - 按索引从下拉列表中选择第一项不起作用。未绑定(bind)方法 select_by_index

python - latex ()同情。如何获得\frac 而不是\\frac

python - 在 Django 中使用 Pillow 压缩图像

添加了 Python MySQL 正则表达式字符串额外的反斜线