0

I want to set the filter on distance by using the point field

i have model shop

class Shop(models.Model):
     distance = models.PositiveIntegerField(null=True)
     Shop_location = models.PointField(null=True)

the shop can define a distance from its location under which its id show to another user. when the user shares its location and the user able to see the shop which satisfies the condition. the condition is that the distance calculated between the user and shop less than or equal to the distance given by the shop

  • Also similar: - https://stackoverflow.com/questions/51506916/geodjango-distance-annotation-on-related-model/51515229#51515229 - https://stackoverflow.com/questions/32609729/geodjango-filter-by-distance-from-a-model-field – John Moutafis Jun 09 '20 at 13:30
  • but I am not able to understand please make code for me I have only one model shop and I want to make a template for the user with a map where they share loction – Sarthak tiwari Jun 10 '20 at 08:19
  • Django filter location distance with dynamic radius it is work when both data save but in my case, I want to use one data save and one is random it is not saved – Sarthak tiwari Jun 10 '20 at 12:05

1 Answers1

0

i haven't tested it but i think this might work
you can do it by Distance object and lookup, geodjango doc explain it well. also you need django F object to get each row's distance value. i have also assumed that your distance that you save in shop model is in kilometer or km

from django.db.models import F
from django.contrib.gis.measure import D


user_location = Point() # your user location should be a standard `Point` object as well

# F object will get each shop's distance
# this query will get you all shops that their distance is less than or equal 
# of the distance between shop_location and user_location
in_distance_shops = Shop.objects.filter(shop_location__distance_lte=(user_location, D(km=F("distance"))))



mahyar
  • 539
  • 1
  • 4
  • 12
  • how the user can share the current location then the shop will print – Sarthak tiwari Jun 08 '20 at 07:00
  • usaully user will post a longtitude and latituder of a location you then will receive these coordination and cast it to django's `Point` object after that you can use this point object. – mahyar Jun 08 '20 at 07:46
  • 2
    I am getting error TypeError: float() argument must be a string or a number, not 'F' – Sarthak tiwari Jun 08 '20 at 12:13