gpt4 book ai didi

django - 不接受使用 django 地理跨度关系过滤查询

转载 作者:行者123 更新时间:2023-12-05 00:32:43 25 4
gpt4 key购买 nike

我正在尝试使用与 geodjango 的地理跨度关系过滤某些项目,但我不明白为什么它不起作用。
考虑这个例子:

class Location(models.Model):
name = models.CharField(max_length=32,)
marker = models.PointField(srid=4326) # the marker
objects = models.GeoManager()

class Item(models.Model):
name = models.CharField(max_length=200)
location = models.ForeignKey(Location)


poly = Polygon(
[(0.02780914306640625, 52.158980248467095),
(0.22350311279296875, 52.158980248467095),
(0.22350311279296875, 52.253657959623055),
(0.02780914306640625, 52.253657959623055),
(0.02780914306640625, 52.158980248467095)]
)

如果我查询(“A location”是数据库中的现有位置)
Item.objects.filter(location__name__exact="A location")

有用。

相反,如果我查询
Item.objects.filter(location__marker__within=poly)

我收到这个错误
---------------------------------------------------------------------------
FieldError Traceback (most recent call last)
/home/mattions/.virtualenvs/ssouk_env/local/lib/python2.7/site-packages/django/core/management/commands/shell.pyc in <module>()
----> 1 Item.objects.filter(location__marker__within=poly)

/home/mattions/.virtualenvs/ssouk_env/local/lib/python2.7/site-packages/django/db/models/manager.pyc in filter(self, *args, **kwargs)
141
142 def filter(self, *args, **kwargs):
--> 143 return self.get_query_set().filter(*args, **kwargs)
144
145 def aggregate(self, *args, **kwargs):

/home/mattions/.virtualenvs/ssouk_env/local/lib/python2.7/site-packages/django/db/models/query.pyc in filter(self, *args, **kwargs)
619 set.
620 """
--> 621 return self._filter_or_exclude(False, *args, **kwargs)
622
623 def exclude(self, *args, **kwargs):

/home/mattions/.virtualenvs/ssouk_env/local/lib/python2.7/site-packages/django/db/models/query.pyc in _filter_or_exclude(self, negate, *args, **kwargs)
637 clone.query.add_q(~Q(*args, **kwargs))
638 else:
--> 639 clone.query.add_q(Q(*args, **kwargs))
640 return clone
641

/home/mattions/.virtualenvs/ssouk_env/local/lib/python2.7/site-packages/django/db/models/sql/query.pyc in add_q(self, q_object, used_aliases, force_having)
1248 else:
1249 self.add_filter(child, connector, q_object.negated,
-> 1250 can_reuse=used_aliases, force_having=force_having)
1251 if force_having:
1252 self.having.end_subtree()

/home/mattions/.virtualenvs/ssouk_env/local/lib/python2.7/site-packages/django/db/models/sql/query.pyc in add_filter(self, filter_expr, connector, negate, trim, can_reuse, process_extras, force_having)
1120 parts, opts, alias, True, allow_many, allow_explicit_fk=True,
1121 can_reuse=can_reuse, negate=negate,
-> 1122 process_extras=process_extras)
1123 except MultiJoin, e:
1124 self.split_exclude(filter_expr, LOOKUP_SEP.join(parts[:e.level]),

/home/mattions/.virtualenvs/ssouk_env/local/lib/python2.7/site-packages/django/db/models/sql/query.pyc in setup_joins(self, names, opts, alias, dupe_multis, allow_many, allow_explicit_fk, can_reuse, negate, process_extras)
1473 if pos != len(names) - 1:
1474 if pos == len(names) - 2:
-> 1475 raise FieldError("Join on field %r not permitted. Did you misspell %r for the lookup type?" % (name, names[pos + 1]))
1476 else:
1477 raise FieldError("Join on field %r not permitted." % name)

FieldError: Join on field 'marker' not permitted. Did you misspell 'within' for the lookup type?

注意
Location.objects.filter(marker__within=poly)

按预期工作

有没有办法在关系中做到这一点?

最佳答案

原来答案是在 geodjango 邮件列表 ( https://groups.google.com/forum/?fromgroups=#!topic/geodjango/wvEJaYX_Cuc ) 上给出的,所以如果有人偶然发现,我也会写在这里

如果您需要在跨度关系上使用 geoQueryset,则还需要在没有空间特征的模型上使用 GeoManager,但是对于具有属性作为类的模型的 ForeignKey。

所以正确的方法是
从 django.contrib.gis.db 导入模型

class Location(models.Model):
name = models.CharField(max_length=32,)
marker = models.PointField(srid=4326) # the marker
objects = models.GeoManager()

class Item(models.Model):
name = models.CharField(max_length=200)
location = models.ForeignKey(Location)
objects = models.GeoManager()

然后是 Item.objects.filter(location__marker__within=poly)按预期工作。

关于django - 不接受使用 django 地理跨度关系过滤查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13035676/

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