How to get the objects in a radius around my current location?

Submitted 10 months, 2 weeks ago
Ticket #366
Views 171
Language Django
Priority Medium
Status Closed

I have a models named Restaurant ,i want to fetch all the restaurant within the 3km radius of user's current location.

I am posting the model and views.

class Restaurant(models.Model):

restaurant_owner = models.ForeignKey(Account, on_delete=models.CASCADE,


null=True, blank=True)

latitude = models.CharField('Latitude', max_length=30, blank=True, null=True)

longitude = models.CharField('Longitude', max_length=30, blank=True, null=True)

location = models.PointField(blank=True, null=True)

owner_name = models.CharField(max_length=200,null=True,blank=True)

restaurant_name = models.CharField(max_length=200,null=True,blank=True)

registration_no = models.CharField(max_length=200,null=True)

registration_date = models.DateField()

registration_mobile = models.CharField(max_length=200,null=True)


class RestView(viewsets.ViewSet):

permission_classes = [IsAuthenticated,OnlyUser]

def list(self, request):


radius = 2




if not user_location:

return Response({"message": "User is not Available","success":False},status=status.HTTP_400_BAD_REQUEST)


#query_set=Restaurant.objects.filter(location__distance_lt=(user_location, Distance(km=radius)))

#query_set=Restaurant.objects.filter(restaurant_owner__location__dwithin=(user_location, Distance(mi=90)))


#query_set=Restaurant.gis.filter(location__dwithin=(user_location, 0.008))

# max_distance = 10000 # distance in meter

# buffer_width = max_distance / 40000000. * 360. / math.cos(point.y / 360. * math.pi)

# buffered_point = point.buffer(buffer_width)

# query_set=Restaurant.objects.filter(location__distance__lte=(user_location, D(m=max_distance)),location__overlaps=buffered_point)


serializer=RestaurantSerializer(data=query_set,many=True,context={'request': self.request})


return Response({"message":,"success":True},status=status.HTTP_200_OK)

except Exception as e:


return Response({"message": str(e),"success":False,},status=status.HTTP_200_OK)

Submitted on Mar 04, 21

Use the code format when you upload the code. it would help other's to check the code easily. please update the code - arun 10 months, 2 weeks ago (Edited)

is this your issue has been resolved? - arun 10 months ago
add a comment

1 Answer


I found interesting answer here answer here(SO) but not exactly 100% correct i think.

One of the best option is convert the raidus into degree and then differentiate that. Take a look on this and let me know if its helped.

Submitted 10 months, 2 weeks ago

I have tried this ,but this gives an error

- ashutoshmishra333 10 months, 2 weeks ago

__init__requred positional arguments exs1,exs2

- ashutoshmishra333 10 months, 2 weeks ago

can you update your code here?.. I can take a look.

- arun 10 months, 1 week ago

Wanna Post Ad?
Reach a large group of audience by posting an ad about your business here. For more details
Drop us a Message