I am working with Django, Django Rest Framework, Django Rest Framework GIS and POSTgis database to create an endpoint that should upload a geografical point and all its related information. I have defined a Model, Serializer and View for that purpose. But when making a request using postman i am getting the following error:

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

I am currently working with:

  • Django==3.2.16
  • djangorestframework==3.12.0
  • djangorestframework-gis==1.0

Model definition:

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'

Serializer:

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',]

View:

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

This is the image of the POSTman request: Postman request

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

And this is the complete error:

 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'>
1

There are 1 answers

0
Kunal On

The problem is that you have the datatype as POINT and you are sending a dictionary datatype. Django is only reading it as a dictionary type and not a POINT type object. https://docs.djangoproject.com/en/3.2/ref/contrib/gis/geos/ You can try manually handling this request, using the data received to create an object first and then moving on to save it in the database. I am assuming POINT is not a native Python Object and is linked with a library.

#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