解决Django Rest Framework TypeError:无法使用以下类型的值设置GeoPoint SpatialProxy(POINT):< class 'dict'>

tjrkku2a  于 2023-06-25  发布在  Go
关注(0)|答案(1)|浏览(97)

我正在使用Django,Django Rest Framework,Django Rest Framework GIS和POSTgis数据库来创建一个端点,该端点应该上传地理点及其所有相关信息。我已经定义了一个模型,序列化器和视图的目的。但是当使用postman发出请求时,我得到以下错误:

TypeError: Cannot set GeoPoint SpatialProxy (POINT) with value of type: <class 'dict'>

我目前正在与:

  • 中文(简体)
  • djangorestframework==3.12.0
  • djangorestframework-gis==1.0

模型定义:

from django.contrib.gis.db import models
from django.utils.translation import gettext_lazy as _
class GeoPoint(models.Model):

    POINT_TYPE_CHOICES = (
       ("limite_cusaf","Límite CUSAF"),
       ("limite_cusaf_di","Límite CUSAF y DI"),
       ("limite_2_di","Límite 2 DI"),
       ("limite_3_di_mas","Límite 3 DI o más"),
       ("otros","Otros"),
    )

    geom = models.PointField(verbose_name=_("Localización"), srid=4326)
    id_cusaf = models.CharField(_("ID CUSAF"), max_length=50)
    code = models.CharField(_("Código Punto"), max_length=50)
    point_type = models.CharField(_("Tipo de Punto"), max_length=50, choices=POINT_TYPE_CHOICES)
    observations = models.TextField(_("Observaciones"), null=True, blank=True)
    
    def __str__(self):
        return self.cod_punto

    class Meta:
        db_table = 'aggregate_geopunto'
        managed = True
        verbose_name = 'Punto'
        verbose_name_plural = 'Puntos'

串行化器:

from rest_framework_gis.serializers import GeoFeatureModelSerializer

class GeoPointSerializer(GeoFeatureModelSerializer):

    class Meta:
        model = GeoPoint
        geo_field = "geom"
        fields = ('id','geom','id_cusaf','code',
        'point_type','observations',)
        read_only_fields = ['id',]

浏览次数:

class GeoPointAPICreate(generics.CreateAPIView):
        authentication_classes = []
        permission_classes = ()
        queryset = GeoPoint.objects.all()
        serializer_class = GeoPointSerializer

这是POSTman请求的图像:

{
    "type": "Feature",
    "geometry": {
        "type": "Point",
        "coordinates": [
            -69.929352,
            18.547504
        ]
    },
    "properties": {
        "id_cusaf": "x001",
        "code": "1",
        "point_type": "limite_cusaf",
        "observations": "observationssdsd"
    }
}

这是完全错误的:

Traceback (most recent call last):
  File "/home/ernesto/Programming/django/virtualenvs/fichacusaf/lib/python3.10/site-packages/rest_framework/serializers.py", line 939, in create
    instance = ModelClass._default_manager.create(**validated_data)
  File "/home/ernesto/Programming/django/virtualenvs/fichacusaf/lib/python3.10/site-packages/django/db/models/manager.py", line 85, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "/home/ernesto/Programming/django/virtualenvs/fichacusaf/lib/python3.10/site-packages/django/db/models/query.py", line 451, in create
    obj = self.model(**kwargs)
  File "/home/ernesto/Programming/django/virtualenvs/fichacusaf/lib/python3.10/site-packages/django/db/models/base.py", line 488, in __init__
    _setattr(self, field.attname, val)
  File "/home/ernesto/Programming/django/virtualenvs/fichacusaf/lib/python3.10/site-packages/django/contrib/gis/db/models/proxy.py", line 74, in __set__
    raise TypeError('Cannot set %s SpatialProxy (%s) with value of type: %s' % (
TypeError: Cannot set GeoPoint SpatialProxy (POINT) with value of type: <class 'dict'>
0tdrvxhp

0tdrvxhp1#

问题是您的数据类型为POINT,而您正在发送dictionary数据类型。Django只是将其作为字典类型读取,而不是POINT类型对象。https://docs.djangoproject.com/en/3.2/ref/contrib/gis/geos/您可以尝试手动处理此请求,首先使用接收到的数据创建一个对象,然后将其保存在数据库中。我假设POINT不是一个原生的Python对象,而是与一个库相链接。

#read all data using request.POST.GET('key')
from django.contrib.gis.geos import Point

x = Point(#all attributes)
Geopoint(geom=x,...) #set other properties too

相关问题