Cannot set MODEL_NAME SpatialProxy (POLYGON) with value of type: <class 'django.contrib.gis.geos.polygon.Polygon'>

流过昼夜 提交于 2021-02-08 08:15:17

问题


When I try an API call to the url of one my models I face the following:

Cannot set Allotment SpatialProxy (POLYGON) with value of type: <class 'django.contrib.gis.geos.polygon.Polygon'>

I check my model's definition and data type in PGadmin.

full log

Traceback:

File "/home/user/MyProjects/forest-venv/lib/python3.5/site-packages/django/core/handlers/exception.py" in inner
  35.             response = get_response(request)

File "/home/user/MyProjects/forest-venv/lib/python3.5/site-packages/django/core/handlers/base.py" in _get_response
  128.                 response = self.process_exception_by_middleware(e, request)

File "/home/user/MyProjects/forest-venv/lib/python3.5/site-packages/django/core/handlers/base.py" in _get_response
  126.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "/home/user/MyProjects/forest-venv/lib/python3.5/site-packages/django/views/decorators/csrf.py" in wrapped_view
  54.         return view_func(*args, **kwargs)

File "/home/user/MyProjects/forest-venv/lib/python3.5/site-packages/rest_framework/viewsets.py" in view
  103.             return self.dispatch(request, *args, **kwargs)

File "/home/user/MyProjects/forest-venv/lib/python3.5/site-packages/rest_framework/views.py" in dispatch
  483.             response = self.handle_exception(exc)

File "/home/user/MyProjects/forest-venv/lib/python3.5/site-packages/rest_framework/views.py" in handle_exception
  443.             self.raise_uncaught_exception(exc)

File "/home/user/MyProjects/forest-venv/lib/python3.5/site-packages/rest_framework/views.py" in dispatch
  480.             response = handler(request, *args, **kwargs)

File "/home/user/MyProjects/forest-venv/lib/python3.5/site-packages/rest_framework/mixins.py" in list
  42.         page = self.paginate_queryset(queryset)

File "/home/user/MyProjects/forest-venv/lib/python3.5/site-packages/rest_framework/generics.py" in paginate_queryset
  173.         return self.paginator.paginate_queryset(queryset, self.request, view=self)

File "/home/user/MyProjects/forest-venv/lib/python3.5/site-packages/rest_framework/pagination.py" in paginate_queryset
  337.         return list(queryset[self.offset:self.offset + self.limit])

File "/home/user/MyProjects/forest-venv/lib/python3.5/site-packages/django/db/models/query.py" in __iter__
  272.         self._fetch_all()

File "/home/user/MyProjects/forest-venv/lib/python3.5/site-packages/django/db/models/query.py" in _fetch_all
  1179.             self._result_cache = list(self._iterable_class(self))

File "/home/user/MyProjects/forest-venv/lib/python3.5/site-packages/django/db/models/query.py" in __iter__
  63.             obj = model_cls.from_db(db, init_list, row[model_fields_start:model_fields_end])

File "/home/user/MyProjects/forest-venv/lib/python3.5/site-packages/django/db/models/base.py" in from_db
  507.         new = cls(*values)

File "/home/user/MyProjects/forest-venv/lib/python3.5/site-packages/django/db/models/base.py" in __init__
  424.                 _setattr(self, field.attname, val)

File "/home/user/MyProjects/forest-venv/lib/python3.5/site-packages/django/contrib/gis/db/models/proxy.py" in __set__
  75.                 instance.__class__.__name__, gtype, type(value)))

Exception Type: TypeError at /api/cutarea-allotment/
Exception Value: Cannot set Allotment SpatialProxy (POLYGON) with value of type: <class 'django.contrib.gis.geos.polygon.Polygon'>

This is the model's definition:

from renter.models import *
from classification_list.models import*
from django.contrib.gis.db import models
from django.contrib.gis.geos import Polygon
...

class All(models.Model): 
    geom = models.PolygonField(geography=True, null=True, blank=True, verbose_name='geom')
    num_kvr = models.ForeignKey(QartalKeys, models.DO_NOTHING, blank=True, null=True)
    allot_num = models.SmallIntegerField(blank=True, null=True)

What is wrong? I didn't find a relevant problem or a useful suggestion for this problem. Thanks.


回答1:


The problem must be related to the geography=True argument of the PolygonField.
Reading the documentation about geography enabled fields we see the following:

The geography type provides native support for spatial features represented with geographic coordinates (e.g., WGS84 longitude/latitude). Unlike the plane used by a geometry type, the geography type uses a spherical representation of its data. Distance and measurement operations performed on a geography column automatically employ great circle arc calculations and return linear units. In other words, when ST_Distance is called on two geographies, a value in meters is returned (as opposed to degrees if called on a geometry column in WGS84).

Since you set the field to expect a geography type object and then you are trying to pass a non geography representation of a Polygon, you get the error.



来源:https://stackoverflow.com/questions/55451675/cannot-set-model-name-spatialproxy-polygon-with-value-of-type-class-django

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!