4

我正在尝试使用与 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)

按预期工作

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

4

1 回答 1

6

事实证明,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)按预期工作。

于 2012-10-24T09:17:50.853 回答