加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

django – 如何使地理领域独一无二?

发布时间:2020-12-20 13:32:15 所属栏目:Python 来源:网络整理
导读:我有这个型号: class Marker(models.Model): location = models.PointField(geography=True,unique=True) 当我尝试通过管理界面添加标记实例时,它会引发一个ValueError: File "django/core/handlers/base.py" in get_response 111. response = callback(re
我有这个型号:

class Marker(models.Model):
    location = models.PointField(geography=True,unique=True)

当我尝试通过管理界面添加标记实例时,它会引发一个ValueError:

File "django/core/handlers/base.py" in get_response
  111. response = callback(request,*callback_args,**callback_kwargs)
File "django/contrib/admin/options.py" in wrapper
  366. return self.admin_site.admin_view(view)(*args,**kwargs)
File "django/utils/decorators.py" in _wrapped_view
  91. response = view_func(request,*args,**kwargs)
File "django/views/decorators/cache.py" in _wrapped_view_func
  89. response = view_func(request,**kwargs)
File "django/contrib/admin/sites.py" in inner
  196. return view(request,**kwargs)
File "django/utils/decorators.py" in _wrapper
  25. return bound_func(*args,**kwargs)
File "django/utils/decorators.py" in bound_func
  21. return func(self,*args2,**kwargs2)
File "django/db/transaction.py" in inner
  209. return func(*args,**kwargs)
File "django/contrib/admin/options.py" in add_view
  937. if form.is_valid():
File "django/forms/forms.py" in is_valid
  124. return self.is_bound and not bool(self.errors)
File "django/forms/forms.py" in _get_errors
  115. self.full_clean()
File "django/forms/forms.py" in full_clean
  272. self._post_clean()
File "django/forms/models.py" in _post_clean
  338. self.validate_unique()
File "django/forms/models.py" in validate_unique
  347. self.instance.validate_unique(exclude=exclude)
File "django/db/models/base.py" in validate_unique
  633. errors = self._perform_unique_checks(unique_checks)
File "django/db/models/base.py" in _perform_unique_checks
  724. if qs.exists():
File "django/db/models/query.py" in exists
  565. return self.query.has_results(using=self.db)
File "django/db/models/sql/query.py" in has_results
  441. return bool(compiler.execute_sql(SINGLE))
File "django/db/models/sql/compiler.py" in execute_sql
  808. sql,params = self.as_sql()
File "django/db/models/sql/compiler.py" in as_sql
  82. where,w_params = self.query.where.as_sql(qn=qn,connection=self.connection)
File "django/db/models/sql/where.py" in as_sql
  91. sql,params = child.as_sql(qn=qn,connection=connection)
File "django/db/models/sql/where.py" in as_sql
  94. sql,params = self.make_atom(child,qn,connection)
File "django/contrib/gis/db/models/sql/where.py" in make_atom
  47. spatial_sql = connection.ops.spatial_lookup_sql(data,lookup_type,params_or_value,lvalue.field,qn)
File "django/contrib/gis/db/backends/postgis/operations.py" in spatial_lookup_sql
  497. '"%s" lookup.' % lookup_type)

Exception Type: ValueError at /admin/coremap/marker/add/
Exception Value: PostGIS geography does not support the "exact" lookup.

根据this,地理类型确实没有确切的字段查找.有没有办法在不使用精确的情况下完成唯一约束?

我正在使用:

> Postgresql 9.1
> PostGIS 1.5
> Django 1.4.3

解决方法

我遇到了类似的问题,但是使用了较新版本的Django(1.10.5),PostGIS(2.0)和Postgres(9.4)(Op的问题是它的答案为4年)

Django向我提出的错误有点不同,但是有关:

ValueError:PostGIS地理不支持“?=”函数/运算符.

事实证明,在这个版本中,Django PostGIS后端使用“?=”运算符来验证某个记录是否已经存在,但PostGIS对地理类型不支持.不确定为什么GeoDjango开发人员没有使用地理和几何类型支持的“=”运算符.

所以我找到的解决方案是通过添加models.py文件的顶部来修补Django PostGIS后端(可能有更优雅的方法来进行这种猴子修补),但对我来说工作得很好……

from django.contrib.gis.db.backends.postgis.operations import (PostGISOperator,PostGISOperations,BILATERAL)
PostGISOperations.gis_operators['exact'] = PostGISOperator(op='=',geography=True,raster=BILATERAL)
PostGISOperations.gis_operators['same_as'] = PostGISOperator(op='=',raster=BILATERAL)

我想如果您仍然在项目中使用旧版本的Django和PostGIS,您可能需要验证是否支持给定的运算符以及如何在特定版本的Django中处理后端运算符.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读