假设我有一个照片模型。在照片模型中,我在我的照片模型中有经纬度字段。如何根据位置坐标过滤Django查询集?
class Photo(models.Model):
photographer = models.ForeignKey(Photographer, related_name = 'shot_owner')
title = models.CharField(max_length=140, blank=True)
description = models.CharField(max_length, blank=True)
longitude = models.DecimalField(max_digits=16, decimal_places = 14, null=True, blank=True)
latitude = models.DecimalField(max_digits=16, decimal_places = 14, null=True, blank=True)
我使用Django Tastypie作为我的rest框架。比方说,用户决定他们希望看到10公里范围内的所有照片。怎么能做到这一点?这里是我的资源如下:
class PhotosNearMe(ModelResource):
photographer = fields.ForeignKey(PhotographerResource, 'photographer', full=True)
class Meta:
queryset = Photo.objects.all()
resource_name = 'photos-near-me'
fields = ['id', 'title', 'description', 'latitude','longitude','photographer']
authentication = BasicAuthentication()
authorization = DjangoAuthorization()
serializer = Serializer(formats=['json'])
include_resource_uri = False
filtering = {
'photographer' : ALL_WITH_RELATIONS,
}
def get_object_list(self, request):
return super(PhotosNearMe, self).get_object_list(request).filter(....)
这是我遇到麻烦。正如我之前提到的,用户将能够向我发送他们的坐标,并且我可以保存它们。喜欢的东西:
lati = bundle.obj.latitude
longi = bundle.obj.longitude
我以后可以使用LAT和长期通过有10公里半径范围内的数据库中的所有图像进行过滤。问题是,如何?我想过滤某种范围?
编辑**
我发现的东西,我也许可以使用,Find items within a certain range of given coordinate
反正我有可以实现这一点?
也许看看[GeoDjango](http://geodjango.org/) – Amadan