openwisp/django-rest-framework-gis

KeyError at /locationlist/ 'dwithin'

Open

#185 opened on Jan 17, 2019

 (1 comment) (0 reactions) (0 assignees)Python (204 forks)batch import
bughacktoberfest

Repository metrics

Stars
 (1,053 stars)
PR merge metrics
 (Avg merge 1h 11m) (1 merged PR in 30d)

Description

When I tried to use DistanceToPointFilter to filter through distance, I got the issue that there is a KeyError about dwithin.

models.py

class Location(models.Model):
    user = models.ForeignKey('UserProfile', null=True, blank=True, on_delete=models.CASCADE)
    address = models.CharField(max_length=255)
    number = models.CharField(max_length=255)
    city = models.CharField(max_length=100)
    state = models.CharField(max_length=100)
    zipcode = models.CharField(max_length=5)
    # point = gis_models.PointField()
    geometry = gis_models.GeometryField()

    class Meta:
        verbose_name = 'Location'
        verbose_name_plural = 'Locations'

    def __str__(self):
        return self.geometry

serializers.py

class LocationSerializer(GeoFeatureModelSerializer):

    class Meta:
        model = Location
        geo_field = 'geometry'
        fields = "__all__"

views.py

class LocationListViewSet(viewsets.GenericViewSet, mixins.ListModelMixin):
    queryset = Location.objects.all()
    serializer_class = LocationSerializer
    distance_filter_convert_meters = True
    distance_filter_field = 'geometry'
    filter_backends = (DistanceToPointFilter,)

Contributor guide