Django按距离排序

编程入门 行业动态 更新时间:2024-10-14 18:15:17
本文介绍了Django按距离排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有模型:

class Vacancy(models.Model): lat = models.FloatField('Latitude', blank = True) lng = models.FloatField('Longitude', blank = True)

$ b $我应该按照距离(距离是无穷远)进行查询排序吗?工作在PosgreSQL,GeoDjango如果需要。

Hi should i make query to sort by distance (distance is infinity) ? Working on PosgreSQL, GeoDjango if it required.

谢谢你。

推荐答案

首先,最好做一个点字段,而不是使用lat和lnt分离:

First of all, it is better to make a point field instead of making lat and lnt separated:

from django.contrib.gis.db import models location = models.PointField(null=False, blank=False, srid=4326, verbose_name="Location")

然后,您可以像下列那样进行过滤:

Then, you can filter it like that:

from django.contrib.gis.geos import * from django.contrib.gis.measure import D distance = 2000 ref_location = Point(1.232433, 1.2323232) res = yourmodel.objects.filter(location__distance_lte=(ref_location, D(m=distance))).distance(ref_location).order_by('distance')

在stackoverlow中已经有几个问题,如果你检查它们就更好。

There are already several questions like that in stackoverlow, better if you'd check them.

更多推荐

Django按距离排序

本文发布于:2023-10-18 15:00:09,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1504566.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:距离   Django

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!