我该如何在 GeoDjango 中编写这个查询? (这是 Django 中用于地理计算的库)

发布于 2024-08-25 13:17:44 字数 425 浏览 2 评论 0原文

现在,我正在使用原始 SQL 来查找当前用户 500 米范围内的人

cursor.execute("SELECT user_id FROM myapp_location WHERE\
       GLength(LineStringFromWKB(LineString(asbinary(utm), asbinary(PointFromWKB(point(%s, %s)))))) < %s"\
       ,(user_utm_easting, user_utm_northing, 500));

我该如何在 GeoDjango 中执行此操作?到处编写自定义 SQL 会有点累。

Right now I'm using raw SQL to find people within 500 meters of the current user.

cursor.execute("SELECT user_id FROM myapp_location WHERE\
       GLength(LineStringFromWKB(LineString(asbinary(utm), asbinary(PointFromWKB(point(%s, %s)))))) < %s"\
       ,(user_utm_easting, user_utm_northing, 500));

How would I do this in GeoDjango? It gets a little tiring writing custom SQL everywhere.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

锦爱 2024-09-01 13:17:44

假设您有适当的模型,

from django.contrib.gis.db import models

class User(models.Model):
    location = models.PointField()
    objects = models.GeoManager()

它看起来像:

User.objects.filter(location__dwithin=(current_user.location, D(m=500)))

但请注意,这样 distance MySQL 不支持查找

Well assuming you have the appropriate model,

from django.contrib.gis.db import models

class User(models.Model):
    location = models.PointField()
    objects = models.GeoManager()

it would look like:

User.objects.filter(location__dwithin=(current_user.location, D(m=500)))

But note that such distance lookups are not supported by MySQL.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文