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 885447
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T12:54:53+00:00 2026-05-15T12:54:53+00:00

Hay i need to hand implemeneting a voting system into a model. I’ve had

  • 0

Hay i need to hand implemeneting a voting system into a model.

I’ve had a huge helping hand from Mike DeSimone making this work in the first place, but i need to expand upon his work.

Here is my current code

View

def show_game(request):
    game = Game.objects.get(pk=1)
    discussions = game.gamediscussion_set.filter(reply_to=None)
    d = {
        'game':game,
        'discussions':discussions
    }
    return render_to_response('show_game', d)

Template

<ul>
    {% for discussion in discussions %}
    {{ discussion.html }}
    {% endfor %}
</ul>

Model

class GameDiscussion(models.Model):
    game = models.ForeignKey(Game)
    message = models.TextField()
    reply_to = models.ForeignKey('self', related_name='replies', null=True, blank=True)
    created_on = models.DateTimeField(blank=True, auto_now_add=True)
    userUpVotes = models.ManyToManyField(User, blank=True, related_name='threadUpVotes')
    userDownVotes = models.ManyToManyField(User, blank=True, related_name='threadDownVotes')

    def html(self):
        DiscussionTemplate = loader.get_template("inclusions/discussionTemplate")
        return DiscussionTemplate.render(Context({
            'discussion': self,
            'replies': [reply.html() for reply in self.replies.all()]
    }))

DiscussionTemplate

<li>
    {{ discussion.message }}
    {% if replies %}
        <ul>
            {% for reply in replies %}
                {{ reply }}
            {% endfor %}
        </ul>
    {% endif %}
</li>

As you can see we have 2 fields userUpVotes and userDownVotes on the model, these will calculate how to order the discussions and replies.

How would i implement these 2 fields to order the replies and discussions based on votes?

Any help would be great!

EDIT

I’ve added a method to my model called vote_difference

    def vote_difference(self):
        return int(self.userUpVotes.count()) - int(self.userDownVotes.count())

I can user this in my templates to get the current vote, however i cannot use this in my view.py file to order by this value, is there anyway to include this value in my view?

EDIT (2)

I’ve slowly getting there, i need to annotate 2 fields and do a calculation on them, however it seems that i cannot do basic maths calculation with annotate.

Any ideas?

    discussions = game.gamediscussion_set.filter(reply_to=None).annotate( score= (Count('userUpVotes') - Count('userDownVotes')) ).order_by('-score')
  • 1 1 Answer
  • 2 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-15T12:54:53+00:00Added an answer on May 15, 2026 at 12:54 pm

    The reddit algorithm is based on the formula for calculating gravity. I found it from this website

    Reddit Algorithm

    let t = (t1 – epoch)
    

    (where t1 is the time the post was made)

    let x be the number of up votes minus the number of down votes.
    

    Then,

    let y be:
    
    • 1 if there are more up votes than down votes,
    • -1 If there are more down voets than up votes,
    • 0 if there are the same number.

    Now Let

    z = max({x,1})
    

    And We Have

    ranking = C log10(z) + yt1
    
    Where C is a constant (C = 45000).
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hay, is it possible to a get a request.session value from a model method
Hay guys, i need help with a REGEX. I have the got the value
Hay, serializers is not returning JSON object make = Make.objects.filter(slug__exact=make) models = Model.objects.filter(make=make).values('slug','name') json_models
Hay, i have a simple model class Manufacturer(models.Model): name = models.CharField() car_count = models.IntegerField()
Hay, I have a Model which looks like this class Person(models.Model): name = models.CharField(blank=False,
Hay guys i need help on a regex. I'm using file_get_contents() to get the
Hay all, i need help making a regex. The string must contain a -
Hay All. I'm currently working on a 'checkout' system, the actual payment and stuff
Hay all im using a simple look to get file names from a dir
Hay, i have an array of objects and i need to sort them (either

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.