Python django投票排序顺序的Hacker新闻算法
我正在使用一个应用程序,并使用以下方法对主页项目进行排序: models.py views.py 我现在想结合上面的代码,以便旧的项目得到了排名下降,但我有麻烦。我不确定相关的代码是否应该放在VoteAwareManager函数中,或者放在最受欢迎的方法中,或者放在其他地方 以下是我尝试过的: 一,。最受欢迎方法中的计算:返回TypeError at/ -:'QuerySet'和'int'不支持的操作数类型当使用随机时间戳只是为了查看是否可以得到结果时,最终我还需要弄清楚如何获得对象时间戳-我是一名初级程序员:Python django投票排序顺序的Hacker新闻算法,python,sql,django,voting,django-voting,Python,Sql,Django,Voting,Django Voting,我正在使用一个应用程序,并使用以下方法对主页项目进行排序: models.py views.py 我现在想结合上面的代码,以便旧的项目得到了排名下降,但我有麻烦。我不确定相关的代码是否应该放在VoteAwareManager函数中,或者放在最受欢迎的方法中,或者放在其他地方 以下是我尝试过的: 一,。最受欢迎方法中的计算:返回TypeError at/ -:'QuerySet'和'int'不支持的操作数类型当使用随机时间戳只是为了查看是否可以得到结果时,最终我还需要弄清楚如何获得对象时间戳-我是
def most_loved(self):
totalscore = self._get_score_annotation()
time_stamp = 20120920
gravity = 1.8
return (totalscore - 1) / pow((time_stamp+2), gravity)
二,。SQL中的计算:在呈现时返回TemplateSyntaxError at/Capture DatabaseError:列投票。时间戳必须出现在GROUP BY子句中,或用于聚合函数行1:…选择COALESCESUMvote,0/EXTRACTHOUR FROM time\u stamp…:
一种选择是尝试更改投票系统以使用,但如果可能的话,我希望使用django投票系统。非常感谢您的帮助。Not perfect省略了-1减法以否定用户自己的投票,但目前这似乎足够有效:
class VoteAwareManager(models.Manager):
""" Get recent top voted items (hacker news ranking algorythm, without the -1 for now since it breaks the calculation as all scores return 0.0)
(p - 1) / (t + 2)^1.5
where p = points and t = age in hours
"""
def _get_score_annotation(self):
model_type = ContentType.objects.get_for_model(self.model)
table_name = self.model._meta.db_table
return self.extra(select={
'score': 'SELECT COALESCE(SUM(vote / ((EXTRACT(EPOCH FROM current_timestamp - created_at)/3600)+2)^1.5),0) FROM %s WHERE content_type_id=%d AND object_id=%s.id' % (Vote._meta.db_table, int(model_type.id), table_name)
})
def most_loved(self):
return self._get_score_annotation().order_by('-score',)
def most_loved(self):
totalscore = self._get_score_annotation()
time_stamp = 20120920
gravity = 1.8
return (totalscore - 1) / pow((time_stamp+2), gravity)
class VoteAwareManager(models.Manager):
""" Get top votes. hot = VoteAwareManager() """
def _get_score_annotation(self):
model_type = ContentType.objects.get_for_model(self.model)
table_name = self.model._meta.db_table
return self.extra(select={
'score': 'SELECT COALESCE(SUM(vote),0 / (EXTRACT(HOUR FROM TIME_STAMP)+2 * 1.8)) FROM %s WHERE content_type_id=%d AND object_id=%s.id' %
(Vote._meta.db_table, int(model_type.id), table_name)
}
)
class VoteAwareManager(models.Manager):
""" Get recent top voted items (hacker news ranking algorythm, without the -1 for now since it breaks the calculation as all scores return 0.0)
(p - 1) / (t + 2)^1.5
where p = points and t = age in hours
"""
def _get_score_annotation(self):
model_type = ContentType.objects.get_for_model(self.model)
table_name = self.model._meta.db_table
return self.extra(select={
'score': 'SELECT COALESCE(SUM(vote / ((EXTRACT(EPOCH FROM current_timestamp - created_at)/3600)+2)^1.5),0) FROM %s WHERE content_type_id=%d AND object_id=%s.id' % (Vote._meta.db_table, int(model_type.id), table_name)
})
def most_loved(self):
return self._get_score_annotation().order_by('-score',)