使用模板在 django 干草堆中渲染点(坐标)



我想做的是通过django-haystack来实现地理空间搜索。在官方指南中,他们建议从普通浮点坐标转换为django.contrib.gis.geos.Point对象。但是,在本指南中,他们没有提到如何在模板中呈现Point。当我尝试这样做时,我得到下一个异常:

 raise SpatialError("Point '%s' doesn't appear to be a GEOS geometry." % geom)
haystack.exceptions.SpatialError: Point 'POINT (49.8448879999999974 40.3779240000000001)' doesn't appear to be a GEOS geometry.

模型类是这样的:

class Shop(models.Model):
    latitude = models.FloatField()
    longitude = models.FloatField()
    def get_location(self):
        return Point(self.latitude, self.longitude)

索引如下所示:

class ShopIndex(indexes.SearchIndex, indexes.Indexable):
    text = indexes.CharField(document=True, use_template=True)
    location = indexes.LocationField(model_attr='get_location', use_template=True)
    def get_model(self):
         return Shop
    def index_queryset(self, using=None):
         return self.get_model().objects.all()

渲染位置的模板如下所示:

{{ object.get_location }}

还有其他方法可以在模板中声明坐标吗?(所以它们可以被Haystack用于地理空间搜索)?或者也许异常描述的问题的任何解决方法?

更新

唯一应该使用位置的地方是下一个搜索查询:

# The point, around which we do want to search
point = Point(lon, lat)
# radius of geospatial search
distance = D(km=rad)
SearchQuerySet().models(models.Location).dwithin('location', point, distance)

您必须将点对象转换为地理几何对象。然后修改用search_indexes.py编写的get_model方法

from django.contrib.gis import geos
def get_location(self):<br>
return geos.fromstr("POINT(%s %s)" %(self.longitude, self.latitude))

最新更新