Reddit-style voting with django

Hay I need to transfer the implementation of the voting system in the model.

I had a lot of help from Mike De Simon doing this work in the first place, but I need to expand 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, they will calculate the order and responses.

How can I implement these 2 fields to organize the answers and discussions based on the votes?

Any help would be great!

EDIT

I added a method to my model called vote_difference

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

, view.py , , ?

(2)

, 2 , , .

?

    discussions = game.gamediscussion_set.filter(reply_to=None).annotate( score= (Count('userUpVotes') - Count('userDownVotes')) ).order_by('-score')
+5
4

reddit . -

Reddit

let t = (t1 – epoch)

( t1 - )

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

let y be:
  • 1, , ,
  • -1 , ,
  • 0, .

z = max({x,1})

ranking = C log10(z) + yt1

Where C is a constant (C = 45000).
+3

, , vote_score.

, , save(), , vote_difference().

, , .

+4

, . reddit code . , , reddit's.

+1

All Articles