Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 779505
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T19:56:11+00:00 2026-05-14T19:56:11+00:00

I have a QuerySet that I wish to pass to a generic view for

  • 0

I have a QuerySet that I wish to pass to a generic view for pagination:

links = Link.objects.annotate(votes=Count('vote')).order_by('-created')[:300]

This is my “hot” page which lists my 300 latest submissions (10 pages of 30 links each). I want to now sort this QuerySet by an algorithm that HackerNews uses:

(p - 1) / (t + 2)^1.5
p = votes minus submitter's initial vote
t = age of submission in hours

Now because applying this algorithm over the entire database would be pretty costly I am content with just the last 300 submissions. My site is unlikely to be the next digg/reddit so while scalability is a plus it is required.

My question is now how do I iterate over my QuerySet and sort it by the above algorithm?

For more information, here are my applicable models:

class Link(models.Model):
    category = models.ForeignKey(Category, blank=False, default=1)
    user = models.ForeignKey(User)
    created = models.DateTimeField(auto_now_add=True)
    modified = models.DateTimeField(auto_now=True)
    url = models.URLField(max_length=1024, unique=True, verify_exists=True)
    name = models.CharField(max_length=512)

    def __unicode__(self):
        return u'%s (%s)' % (self.name, self.url)

class Vote(models.Model):
    link = models.ForeignKey(Link)
    user = models.ForeignKey(User)
    created = models.DateTimeField(auto_now_add=True)

    def __unicode__(self):
        return u'%s vote for %s' % (self.user, self.link)

Notes:

  1. I don’t have “downvotes” so just the presence of a Vote row is an indicator of a vote or a particular link by a particular user.

EDIT

I think I have been overcomplicating things and found out a nifty little piece of code:

links = Link.objects.annotate(votes=Count('vote')).order_by('-created')[:300]
for link in links:
    link.popularity = ((link.votes - 1) / (2 + 2)**1.5)

But for the life of me I cannot get that to translate to my templates:

{% for link in object_list %}
    Popularity: {{ link.popularity }}
{% endfor %}

Why is it not showing up? I know popularity is working because:

print 'LinkID: %s - Votes: %s - Popularity: %s' % (link.id, link.votes, link.popularity)

returns what I’d expect in console.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T19:56:12+00:00Added an answer on May 14, 2026 at 7:56 pm

    While was unable to calculate over a QuerySet, instead I had to convert into a list of sorts

    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)
    

    Not optimal but it works. I can’t use my lovely object_list generic view with it’s automatically pagination and have to resort to doing it manually but it’s a fair compromise to having a working view…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For example, let's assume that i have two QuerySEt objects: queryset1 = my_model1.objects.all().order_by('-created') queryset2
So I have a view that does a queryset and returns a simple list:
I have to work with a queryset, that is already filtered, eg. qs =
I have a queryset containing some objects. Depending on some case or the other
I have a list of objects from a django queryset, e.g. my_list = MyObject.objects.filter(variable=something)
I have the following user resource: class UserResource(ModelResource): class Meta: queryset = User.objects.all() resource_name
I have a simple form which uses a queryset that is set dynamically: class
Let's say that I have a list of movie ids in a QuerySet in
I have a django queryset that returns a list of values: [(client pk, timestamp,
I have such QuerySet in Django. j = piosenki.objects.select_related('projekt__postac').distinct().exclude(typ__in=[live, remix]).filter(piosenki__plyta__status=1, piosenki__plyta__typ='s') .extra(select={ 'ocena' :

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.