我有这个模型:
class Marker(models.Model):
location = models.PointField(geography=True, unique=True)
当我尝试通过管理界面添加一个Marker实例时,它会引发一个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.
根据这一点,geography
类型确实没有exact
字段查找。有没有办法实现unique
约束而不使用exact
?
我用:
- Postgresql 9.1
- PostGIS 1.5 Django
- 3
我遇到了类似的问题,但是使用了较新版本的Django (1.10.5), PostGIS(2.0)和Postgres (9.4) (Op的问题从这个答案到4年)
Django给我的错误有点不同,但相关:
ValueError: PostGIS geography does not support the "~=" function/operator.
原来Django PostGIS后端,在这个版本中,使用"~="操作符来验证某个记录是否已经存在,但是PostGIS在地理类型上不支持这一点。不知道为什么geojango开发者没有使用地理和几何类型都支持的"="运算符。
所以我找到的解决方案是通过在models.py文件的顶部添加monkey-patch Django PostGIS后端(也许有更优雅的方法来做这个monkey-patch),但对我来说很好…
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中是如何处理的。
我不知道你是否可以在Django中直接这样做,或者你是否需要在事后添加索引,但是PostgreSQL确实支持唯一函数索引。
你可以使用如下命令来强制一个唯一的几何图形:
CREATE UNIQUE INDEX tbl_geometry_idx_u ON mytable(geometry_to_text(mygeom));
这将在几何列的文本表示上创建一个唯一的索引。在大小上可能有限制(我不期望索引能很好地处理toast值),但如果你的几何字段中有那么多点,那么你就会遇到独特约束的问题。