0

The following code will filter properly:

def nearby(self, latitude, longitude, proximity=None):
        """Get nearby Merchants.

        Custom queryset method for getting merchants associated with
        nearby places.

        Returns:
            A queryset of ``Merchant`` objects.

        """
        point = Point(latitude, longitude)

        # we query for location nearby for places first and then
        # annotate with distance to the same place
        return self.filter(
            places__location__distance_lte=(point, D(ft=proximity))
        ).annotate(distance=models.Min(Distance('places__location', point)))

but the value returned by

models.Min(Distance('places__location', point))

is in meters.

Is it possible to get it in feet?

John Moutafis
  • 22,254
  • 11
  • 68
  • 112
DmitrySemenov
  • 9,204
  • 15
  • 76
  • 121

1 Answers1

1

If we take a look at the Distance documentation we will see this:

Because the distance attribute is a Distance object, you can easily express the value in the units of your choice.

Although this is not entirely true:

Those two aren't that closely related to each other. Docs do indeed say that it "returns" Distance object, but there is an extra step:

django.contrib.gis.db.models.functions.Distance is a database function, it takes two expressions (that may include database field names) and returns a Func object that can be used as a part of a query.

So simply put, it needs to be executed in a database. It will calculate distance using database function (e.g. postgis ST_Distance) and then bring it back as a django.contrib.gis.measure.Distance object


Judging from the above you can do:
... .annotate(distance=models.Min(Distance('places__location', point).ft))
John Moutafis
  • 22,254
  • 11
  • 68
  • 112