无法将MODEL_NAME SpatialProxy(POLYGON)设置为以下类型的值:<class'django.contrib.gis.geos.polygon.polygon'=“”>

时间:2019-04-01 09:15:10

标签: django postgis geodjango

当我尝试对模型的URL进行API调用时,会遇到以下问题:

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

我在PGadmin中检查模型的定义和数据类型。

完整日志

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

这是模型的定义:

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)

怎么了?我没有发现相关问题或对该问题的有用建议。谢谢。

1 个答案:

答案 0 :(得分:0)

该问题必须与geography=True的{​​{1}}参数有关。
阅读documentation about geography enabled fields后,我们将看到以下内容:

  

地理类型为由地理坐标表示的空间特征(例如WGS84经度/纬度)提供了本机支持。与几何类型所使用的平面不同,地理类型使用了其球形表示数据。在地理列上执行的距离和测量操作自动采用大​​圆弧计算并返回线性单位。换句话说,在两个地理位置上调用ST_Distance时,将返回以米为单位的值(如果在WGS84中的几何列上调用则为度)。

由于您将字段设置为期望使用PolygonField类型的对象,然后尝试传递多边形的非地理表示形式,因此会出现错误。