geopyを使用できます
from geopy import distance
_, ne = g.geocode('Newport, RI')
_, cl = g.geocode('Cleveland, OH')
distance.distance(ne, cl).miles
# 538.37173614757057
少し最適化するには、ユーザー オブジェクトをフィルタリングして、最初に近くのユーザーの概算を取得します。この方法では、データベース内のすべてのユーザーをループする必要はありません。この概算見積もりはオプションです。プロジェクトのすべての要件を満たすには、追加のロジックを作成する必要がある場合があります。
#The location of your user.
lat, lng = 41.512107999999998, -81.607044999999999
min_lat = lat - 1 # You have to calculate this offsets based on the user location.
max_lat = lat + 1 # Because the distance of one degree varies over the planet.
min_lng = lng - 1
max_lng = lng + 1
users = User.objects.filter(lat__gt=min_lat, lat__lt=max__lat, lat__gt=min_lat, lat__lt=max__lat)
# If not 20 fall back to all users.
if users.count() <= 20:
users = User.objects.all()