gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 05:25:29 27 4
gpt4 key购买 nike

我有这个模型:

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.

根据 this , geography类型真的没有 exact字段查找。有什么办法可以完成 unique不使用约束 exact ?

我正在使用:
  • 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 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 中如何处理后端运算符。

    关于django - 如何使地理领域独一无二?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14077993/

    27 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com