14

做我的第一个真正的 Django 项目,需要指导。

背景: 我的项目是一个 reddit 克隆。用户提交链接+文本。访客赞成或反对。有一个 social_ranking 算法,每 2 分钟运行一次,作为后台脚本,根据网络投票和内容的新鲜度重新排列所有提交的内容。相当香草的东西。

问题: Ordering byvotes无法正常工作,因为votes被初始化为None而不是0. 这会导致有None投票的提交排名低于有反对票的提交。我已经调试了这个问题好几天了——没有运气。

细节: 我已经覆盖了我的模型的模型管理器以将Sum聚合函数注释到查询集,然后按“社会等级”和投票对所述查询集进行排序。

下面是我的models.py。我正在使用Django 1.5,因此您在此处看到的某些内容可能与 1.8 不对应(例如get_query_setvs get_queryset):

class LinkVoteCountManager(models.Manager):
    def get_query_set(self):
        return super(LinkVoteCountManager, self).get_query_set().annotate(votes=Sum('vote__value')).order_by('-rank_score', '-votes') 

class Link(models.Model):
    description = models.TextField(_("Write something"))
    submitter = models.ForeignKey(User)
    submitted_on = models.DateTimeField(auto_now_add=True)
    rank_score = models.FloatField(default=0.0)
    url = models.URLField(_("Link"), max_length=250, blank=True)

    with_votes = LinkVoteCountManager() 
    objects = models.Manager() 

    def __unicode__(self): 
        return self.description

    def set_rank(self):
        # Based on reddit ranking algo at http://amix.dk/blog/post/19588
        epoch = datetime(1970, 1, 1).replace(tzinfo=None)
        netvotes = self.votes # 'NONE' votes are messing up netvotes amount.
        if netvotes == None:
            netvotes = 0
        order = log(max(abs(netvotes), 1), 10)
        sign = 1 if netvotes > 0 else -1 if netvotes < 0 else 0
        unaware_submission = self.submitted_on.replace(tzinfo=None)
        td = unaware_submission - epoch 
        epoch_submission = td.days * 86400 + td.seconds + (float(td.microseconds) / 1000000)
        secs = epoch_submission - 1432201843
        self.rank_score = round(sign * order + secs / 45000, 8)
        self.save()

class Vote(models.Model):
    voter = models.ForeignKey(User)
    link = models.ForeignKey(Link)
    value = models.IntegerField(null=True, blank=True, default=0)

    def __unicode__(self):
        return "%s gave %s to %s" % (self.voter.username, self.value, self.link.description)

如果需要,以下是我的views.py中的相关部分:

class LinkListView(ListView):
    model = Link
    queryset = Link.with_votes.all()
    paginate_by = 10

    def get_context_data(self, **kwargs):
        context = super(LinkListView, self).get_context_data(**kwargs)
        if self.request.user.is_authenticated():
            voted = Vote.objects.filter(voter=self.request.user)
            links_in_page = [link.id for link in context["object_list"]]
            voted = voted.filter(link_id__in=links_in_page)
            voted = voted.values_list('link_id', flat=True)
            context["voted"] = voted
        return context

class LinkCreateView(CreateView):
    model = Link
    form_class = LinkForm

    def form_valid(self, form):
        f = form.save(commit=False)
        f.rank_score=0
        f.with_votes = 0
        f.category = '1'
        f.save()
        return super(CreateView, self).form_valid(form)

谁能阐明我需要做些什么来解决“ None”问题?提前致谢。

4

2 回答 2

36

只是碰到了同一堵墙,尽管我选择None通过将条目排除在结果之外来忽略条目。猜你不想要那个。

顺便说一句,这个问题有同样的问题Annotating a Sum 结果为 None 而不是零

至于该问题的答案中指出的使用自定义 sql 以外的解决方案,您可以改用 Django 1.8 并使用 Django 的错误跟踪器中打开的票中指出的解决方案超过 6 年(!)https:// code.djangoproject.com/ticket/10929

Coalesce(Sum('field'), 0)

所以你的经理将是:

class LinkVoteCountManager(models.Manager):
    def get_query_set(self):
        return super(LinkVoteCountManager, self).get_query_set().annotate(
            votes=Coalesce(Sum('vote__value'), 0)
        ).order_by(
            '-rank_score', 
            '-votes'
        )

PS:我没有测试代码,因为我自己没有使用 Django 1.8。

于 2015-10-09T20:43:53.650 回答
7

您也可以替换该行

netvotes = self.votes

netvotes = self.votes or 0

您现在可以删除 if 语句。

在许多其他语言中,它的作用是返回非虚假值(None,0,“”),或者在这种特殊情况下返回最后一个值“0”。

于 2016-12-02T00:06:16.317 回答