错误在POST Django-Tastypie方法中添加几何体

时间:2014-11-07 13:31:18

标签: tastypie geodjango

我有以下具有空间支持的模型:

class Incidencia(gismodels.Model):
    nombre = models.CharField("Nombre", max_length=45)
    descripcion = models.TextField("Descripción", max_length=100)
    multimedia = models.ImageField("Evidencia multimedia", upload_to="evidencia")
    fecha = models.DateTimeField("Fecha de reporte",default=datetime.now,null=True, blank=True)

    geom = gismodels.PointField("Ubicación")
    objects = gismodels.GeoManager()

    class Meta:
        verbose_name = 'Incidencia'
        verbose_name_plural = "Incidencias"
        ordering = ["-fecha", "-nombre"]

然而,当试图添加一个incidencia,如:

<form enctype="multipart/form-data" method="post" id="add_incidencia" name="add_incidencia" id="addincidencia">
    Nombre <input type="text" name="nombre">
    Descripcion <textarea name="descripcion"></textarea>
    Imagen <input type="file" name="multimedia">
    Geometria <input type="text" name="geom">
    <input type="submit" value="Submit">
</form>

的值为:

curl -F "nombre=Incidencia 1" "descripcion=descripcion incidencia" -F "multimedia=@/home/samtux/valla.png" -F "geom={"coordinates":[-74.05016606562,4.6821406310872],"type":"Point"}" http://0.0.0.0:8000/api/v1/incidencia/

``` 它返回以下错误消息:

{"error_message": "String or unicode input unrecognized as WKT EWKT, and HEXEWKB.", "traceback": "Traceback (most recent call last):\n\n  File \"/home/samtux/.virtualenvs/sdaVirtual/lib/python2.7/site-packages/tastypie/resources.py\", line 201, in wrapper\n    response = callback(request, *args, **kwargs)\n\n  File \"/home/samtux/.virtualenvs/sdaVirtual/lib/python2.7/site-packages/tastypie/resources.py\", line 432, in dispatch_list\n    return self.dispatch('list', request, **kwargs)\n\n  File \"/home/samtux/.virtualenvs/sdaVirtual/lib/python2.7/site-packages/tastypie/resources.py\", line 464, in dispatch\n    response = method(request, **kwargs)\n\n  File \"/home/samtux/.virtualenvs/sdaVirtual/lib/python2.7/site-packages/tastypie/resources.py\", line 1340, in post_list\n    updated_bundle = self.obj_create(bundle, **self.remove_api_resource_names(kwargs))\n\n  File \"/home/samtux/.virtualenvs/sdaVirtual/lib/python2.7/site-packages/tastypie/resources.py\", line 2104, in obj_create\n    return self.save(bundle)\n\n  File \"/home/samtux/.virtualenvs/sdaVirtual/lib/python2.7/site-packages/tastypie/resources.py\", line 2250, in save\n    bundle.obj.save()\n\n  File \"/home/samtux/.virtualenvs/sdaVirtual/lib/python2.7/site-packages/django/db/models/base.py\", line 545, in save\n    force_update=force_update, update_fields=update_fields)\n\n  File \"/home/samtux/.virtualenvs/sdaVirtual/lib/python2.7/site-packages/django/db/models/base.py\", line 573, in save_base\n    updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)\n\n  File \"/home/samtux/.virtualenvs/sdaVirtual/lib/python2.7/site-packages/django/db/models/base.py\", line 654, in _save_table\n    result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)\n\n  File \"/home/samtux/.virtualenvs/sdaVirtual/lib/python2.7/site-packages/django/db/models/base.py\", line 687, in _do_insert\n    using=using, raw=raw)\n\n  File \"/home/samtux/.virtualenvs/sdaVirtual/lib/python2.7/site-packages/django/db/models/manager.py\", line 232, in _insert\n    return insert_query(self.model, objs, fields, **kwargs)\n\n  File \"/home/samtux/.virtualenvs/sdaVirtual/lib/python2.7/site-packages/django/db/models/query.py\", line 1510, in insert_query\n    query.insert_values(fields, objs, raw=raw)\n\n  File \"/home/samtux/.virtualenvs/sdaVirtual/lib/python2.7/site-packages/django/db/models/sql/subqueries.py\", line 212, in insert_values\n    value = getattr(obj, field.attname)\n\n  File \"/home/samtux/.virtualenvs/sdaVirtual/lib/python2.7/site-packages/django/contrib/gis/db/models/proxy.py\", line 40, in __get__\n    geom = self._klass(geom_value)\n\n  File \"/home/samtux/.virtualenvs/sdaVirtual/lib/python2.7/site-packages/django/contrib/gis/geos/geometry.py\", line 79, in __init__\n    raise ValueError('String or unicode input unrecognized as WKT EWKT, and HEXEWKB.')\n\nValueError: String or unicode input unrecognized as WKT EWKT, and HEXEWKB.\n"}

如何将几何体发送到新事件,因为我需要实现此多部分表单来上传图像?

1 个答案:

答案 0 :(得分:0)

我已经看到错误是由MultipartResource引起的,因为几何体是作为字符串处理的。所以我从格式字段中做了一些修改,形式为WKT&#34; POINT(lat long)&#34;然后将其转换为GeoJSON:

class MultipartResource(object):
    def deserialize(self, request, data, format=None):
        if not format:
            format = request.META.get('CONTENT_TYPE', 'application/json')

        if format == 'application/x-www-form-urlencoded':
            return request.POST

        if format.startswith('multipart'):
            data = request.POST.copy()
            data.update(request.FILES)
            geom = simplejson.loads(GEOSGeometry(request.POST["geom"]).geojson) 
            request.POST["geom"] = geom
            data.update(request.POST)
            return data

        return super(MultipartResource, self).deserialize(request, data, format)