用django进行投票 [英] reddit style voting with django
问题描述
我已经从Mike DeSimone获得了巨大的帮助,这使得这项工作在第一位,但是我需要扩展他的工作。
这是我现在的代码
查看
def show_game(request):
game = Game.objects.get(pk = 1)
discussion = game.gamediscussion_set.filter(reply_to = None)
d = {
'game':game,
'discussion':discussion
}
return render_to_response ('show_game',d)
模板
< ul>
{%讨论讨论%}
{{discussion.html}}
{%endfor%}
< / ul>
模型
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在self.replies.all()]
}))
DiscussionTemplate
< li>
{{discussion.message}}
{%if replies%}
< ul>
{%在回复中回复%}
{{reply}}
{%endfor%}
< / ul>
{%endif%}
< / li>
如您所见,我们在模型中有2个字段userUpVotes和userDownVotes,这些将计算如何排序讨论和答复。
我将如何实现这2个字段以基于投票命令回复和讨论?
任何帮助都会很棒!
编辑
我已经添加一个叫做vote_difference的模型的方法
def vote_difference(self):
return int(self.userUpVotes.count )) - int(self.userDownVotes.count())
我可以在我的模板中使用这个得到当前的投票,但是我不能在我的view.py文件中使用这个值来排序这个值,是否还有这个值在我的视图中?
编辑(2)
我慢慢到达那里,我需要注释2个字段并对它们进行计算,但是似乎我不能做基础数学计算wi注释。
任何想法?
discussion = game.gamediscussion_set .filter(reply_to = None).annotate(score =(Count('userUpVotes')) - Count('userDownVotes')).order_by(' - score')
reddit算法基于计算重力的公式。我从本网站发现它
Reddit算法
let t =(t1 - epoch)
(其中t1是发布时间)
让x是最多投票数减去投票数。
然后,
code> let y be:
- 1如果有更多如果有更多的失败,而不是投票,
- 0如果有相同的数字。
-
现在让
z = max ({x,1})
我们有
ranking = C log10(z)+ yt1
其中C是常数(C = 45000)。
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')
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).
这篇关于用django进行投票的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!