Django: sorting objects by distance

Django: sorting objects by distance

I have this model

I have this model

class Company(models.Model):
name = models.CharField(max_length = 50)
description = models.TextField()
latitude = models.FloatField()
longitude = models.FloatField()
owner = models.ForeignKey(User, on_delete = models.CASCADE, related_name = "company_owner")
category = models.ForeignKey(Category, on_delete = models.CASCADE)
def __str__(self):
class Meta:
    verbose_name_plural = "Companies"

def get_absolute_url(self): return reverse('category_list') #if want to redirect to its detail page then # return reverse('company_detail' ,kwargs = {'pk' :})

def get_distance(self): ip = get('').text reader = geoip2.database.Reader('categories/GeoLite2-City.mmdb') response = current_lat = response.location.latitude current_lon = response.location.longitude comp_lat = self.latitude comp_lon = self.longitude R = 6373.0

lat1 = radians(current_lat)
lon1 = radians(current_lon)
lat2 = radians(comp_lat)
lon2 = radians(comp_lon)

dlon = lon2 - lon1
dlat = lat2 - lat1

a = sin(dlat / 2)**2 + cos(lat1) * cos(lat2) * sin(dlon / 2)**2
c = 2 * atan2(sqrt(a), sqrt(1 - a))

distance = R * c

I have got the distance between the user location and company location from get_distance() function. But how do I sort the distance in ascending order? Since the distance differs from various location of the user I can't store the distance in database. I want to print the objects sorted in ascending order by distance

Angular 9 Tutorial: Learn to Build a CRUD Angular App Quickly

What's new in Bootstrap 5 and when Bootstrap 5 release date?

Brave, Chrome, Firefox, Opera or Edge: Which is Better and Faster?

How to Build Progressive Web Apps (PWA) using Angular 9

What is new features in Javascript ES2020 ECMAScript 2020

Python Django Tutorial | Django Course

🔥Intellipaat Django course: 👉This Python Django tutorial will help you learn what is django web development &...

How to Integrate Django to any Javascript Front End // Django to React // Django to Angular

Learn the basic technique to Integrate Django to any Javascript Front End // Django to React // Django to Angular


Well i used django ... i want to know after completing all the coding stuff, can i make all the codes online ?