Django Filter Backend按纬度和经度过滤

时间:2017-09-25 15:08:04

标签: django django-rest-framework django-filter

我正在使用django-google-maps来获取某个地点的位置。 在模型中我有这个领域:

models.py

class PlaceLocation(models.Model):
    name = models.CharField(max_length=200)
    address = map_fields.AddressField(max_length=200)
    geolocation = map_fields.GeoLocationField(max_length=100)

当我添加地址时,它会自动以下列格式查找纬度和经度:44.4385334,26.005750199999966

我想使用django过滤后端按纬度和经度进行过滤,但我无法将值分开。

filters.py

class LocationFilter(django_filters.FilterSet):
    ids = NumberInFilter(name='id', lookup_expr='in')
    geolocation = django_filters.NumberFilter(name='geolocation', lookup_expr='iexact')

如果我访问:

'http://127.0.0.1:8000/locations/places/'它会告诉我所有的地方。

'http://127.0.0.1:8000/locations/places/?geolocation=44.4385334,26.005750199999966'它给了我一个空列表。

如何将值分开,以便我可以像这样过滤: 'http://127.0.0.1:8000/locations/places/?lat=44.4385334&long=26.005750199999966'

1 个答案:

答案 0 :(得分:0)

最终这起作用了:

class LocationFilter(django_filters.FilterSet):
    ids = NumberInFilter(name='id', lookup_expr='in')
    geolocation = django_filters.CharFilter(name='geolocation', lookup_expr='exact')

    class Meta:
        model = Location
        fields = ('ids', 'geolocation')