我有这个型号:
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, *args, **kwargs)
File "django/contrib/admin/sites.py" in inner
196. return view(request, *args, **kwargs)
File "django/utils/decorators.py" in _wrapper
25. return bound_func(*args, **kwargs)
File "django/utils/decorators.py" in _wrapped_view
91. response = view_func(request, *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,geography
类型实际上没有exact
字段查找。有没有办法在不使用unique
的情况下完成exact
约束?
我正在使用:
答案 0 :(得分:1)
我遇到了类似的问题,但是使用了更新版本的Django(1.10.5),PostGIS(2.0)和Postgres(9.4)(Op&#39的问题,答案是4年)
Django向我提出的错误有点不同,但是有关:
ValueError: PostGIS geography does not support the "~=" function/operator.
事实证明,在这个版本中,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='=',
geography=True,
raster=BILATERAL)
我想如果您仍然在项目中使用旧版本的Django和PostGIS,您可能需要验证是否支持给定的运算符以及如何在特定版本的Django中处理后端运算符。
答案 1 :(得分:0)
我不知道你是否可以直接在Django中执行此操作,或者是否需要在事后添加索引,但PostgreSQL确实支持唯一的功能索引。
您可以使用以下内容强制执行唯一的几何体:
CREATE UNIQUE INDEX tbl_geometry_idx_u ON mytable(geometry_to_text(mygeom));
这将在几何列的文本表示上创建唯一索引。可能存在大小限制(我不希望索引对于烘焙值有效)但如果在几何字段中有那么多点,那么您将遇到具有唯一约束的问题。