无法使用以下类型的值设置 MODEL_NAME SpatialProxy (POLYGON):<class 'django.contrib.gis.geos.polygon.Polygon'> [英] Cannot set MODEL_NAME SpatialProxy (POLYGON) with value of type: <class 'django.contrib.gis.geos.polygon.Polygon'>

查看:70
本文介绍了无法使用以下类型的值设置 MODEL_NAME SpatialProxy (POLYGON):<class 'django.contrib.gis.geos.polygon.Polygon'>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试对我的模型的 URL 进行 API 调用时,我面临以下情况:

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

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

I check my model's definition and data type in 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)

怎么了?我没有找到有关此问题的相关问题或有用的建议.谢谢.

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

推荐答案

问题必须与 PolygonFieldgeography=True 参数有关.
阅读关于geography<的文档/code> 启用字段 我们看到以下内容:

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

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

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).

由于您将字段设置为期望 geography 类型的对象,然后您尝试传递多边形的非地理表示,因此您会收到错误.

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.

这篇关于无法使用以下类型的值设置 MODEL_NAME SpatialProxy (POLYGON):&lt;class 'django.contrib.gis.geos.polygon.Polygon'&gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆