Django order_by 相关模型的方法
我想为每个单独的查询按“距离”订购“事件”查询集
鉴于以下情况,我该如何实现?
class Venue(models.Model):
latitude = models.FloatField(max_length=10)
longitude = models.FloatField(max_length=10)
def distance(self, query_coordinates):
"Calculates distance on a query by query basis"
venue_coords = self.latitude, self.longitude
distance_to_venue = distance.distance(query_coordinates, house_coords) # a function which outputs distance in miles
return distance_to_venue
class Event(models.Model):
venue = models.ForeignKey(Venue, related_name='shows')
在阅读了一些相关的 stackoverflow 问题后,我一直在仔细阅读注释文档,但不知道如何让它适合我的情况。我怎样才能有效地做到这一点?
提前致谢。
I'd like to order an "Event" queryset by "distance" for each individual query
Given the following, how can I pull it off?
class Venue(models.Model):
latitude = models.FloatField(max_length=10)
longitude = models.FloatField(max_length=10)
def distance(self, query_coordinates):
"Calculates distance on a query by query basis"
venue_coords = self.latitude, self.longitude
distance_to_venue = distance.distance(query_coordinates, house_coords) # a function which outputs distance in miles
return distance_to_venue
class Event(models.Model):
venue = models.ForeignKey(Venue, related_name='shows')
I've been pouring over the annotate docs after reading some related stackoverflow questions, but can't figure out how to get it to work for my situation. How can I do this efficiently??
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你无法做到这一点,django 不支持按模型方法排序。
要处理地理空间数据,请尝试 GeoDjango (http://docs.djangoproject.com/en/dev/ref/contrib/gis/)。
You can't pull it off, django doesn't support ordering by model methods.
For working with geo-spatial data try GeoDjango (http://docs.djangoproject.com/en/dev/ref/contrib/gis/).