cursor = connection.cursor()
cursor.execute(
    """SELECT id, ( 
        3959 * acos( cos( radians(37) ) * cos( radians( lat ) ) * 
        cos( radians( lng ) - radians(-122) ) + sin( radians(37) ) * 
        sin( radians( lat ) ) ) )
        AS distance FROM stores HAVING distance < 25
        ORDER BY distance LIMIT 0 , 20;"""
    )
ids = [row[0] for row in cursor.fetchall()]
return MyModel.filter(id__in=ids)
Filter zipcodes by proximity in Django with the Spherical Law of Cosines