[英]How can I filter posts rendered to a user based on that user's location in Django?
I'm building a Django social networking site, so I basically want to show posts in news feed in a way like if someone is in New York, he should get posts from fellow network users and possibly nearby places.我正在构建一个 Django 社交网站,所以我基本上想在新闻提要中显示帖子,就像有人在纽约一样,他应该从其他网络用户和可能附近的地方获取帖子。 How can I achieve this.
我怎样才能做到这一点。 Any help is appreciated
任何帮助表示赞赏
Have a look at the GIS QuerySet documentation pages in the Django docs: https://docs.djangoproject.com/en/4.0/ref/contrib/gis/geoquerysets/查看 Django 文档中的 GIS QuerySet 文档页面: https://docs.djangoproject.com/en/4.0/ref/contrib/gis/geoquerysets/
If you have latitudes & longitudes stored as a geometry field, you can use dwithin
to filter users within a certain distance:如果您将纬度和经度存储为几何字段,则可以使用
dwithin
过滤一定距离内的用户:
within: Returns models where the distance to the geometry field from the lookup geometry are within the given distance from one another.
范围内:返回从查找几何到几何字段的距离在彼此之间给定距离内的模型。 Note that you can only provide Distance objects if the targeted geometries are in a projected system.
请注意,如果目标几何体位于投影系统中,您只能提供距离对象。 For geographic geometries, you should use units of the geometry field (eg degrees for WGS84).
对于地理几何,您应该使用几何字段的单位(例如 WGS84 的度数)。
Example:
Zipcode.objects.filter(poly__dwithin=(geom, D(m=5)))
示例:
Zipcode.objects.filter(poly__dwithin=(geom, D(m=5)))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.