links = Link.objects.select_related().annotate(votes=Count('vote'))
for link in links:
    delta_in_hours = (int(datetime.now().strftime('%s')) - int(link.created
        .strftime('%s'))) / 3600
link.popularity = (link.votes - 1) / (delta_in_hours + 2) ** 1.5
links = sorted(links, key=lambda x: x.popularity, reverse=True)
Django: применение вычислений к набору запросов