在Django教程中过滤可选择的轮询会导致索引中的轮询复制 [英] Filtering out choiceless polls in the Django tutorial causes polls in the index to duplicate

查看:178
本文介绍了在Django教程中过滤可选择的轮询会导致索引中的轮询复制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经编辑了教程索引视图看起来像这样:

  class IndexView(generic.ListView):
template_name ='polls /index.html'
context_object_name ='latest_poll_list'

def get_queryset(self):

返回最后五次发布的投票(不包括那些集

return Poll.objects.filter(
pub_date__lte = timezone.now(),choice__choice_text__isnull = False
).order_by(' - pub_date')[:5]

但现在我的索引看起来像这样:




  • 你到底是什么

  • 你到底是什么

  • 你到底是什么

  • 什么事?

  • 什么事?



这是怎么回事?我应该如何解决这个问题?



(ps我不知道使用过滤器,所以我从这个问题这是为什么它不工作?双下划线 __ 符号如何工作在过滤器?)



编辑:我已经查看过管理员视图,只有DB中的每个轮询似乎只有一个(我没有直接检查) ,但我相信,它的设置方式我不能使用相同的ID进行多次民意测验(情况如此,所有的你是什么?民意调查有ID 2,所有的Whats up

解决方案

添加 distinct() (如 Wolf 建议) ods确实有效。我认为这个问题的关键在于最终的DB查询返回最近的投票组合( pub_date__lte = timezone.now())并且使用非空选项轮询( choice__choice_text__isnull = False )不管重叠。



我不知道有更好的方法阻止在根目录,而不是使用 distinct()。我试图链接过滤器,似乎没有什么区别。



结果是,正确的解决方案: p>

我做了一个搜索的解释,看起来当一个查询跨多个表,它可以返回重复。distinct是这里的正确解决方案。

- Ludovic Viaud comment


I've edited the code from the tutorial for the index view to look like this:

class IndexView(generic.ListView):
    template_name = 'polls/index.html'
    context_object_name = 'latest_poll_list'

    def get_queryset(self):
        """
        Return the last five published polls (not including those set to be
        published in the future, or those without choices).
        """
        return Poll.objects.filter(
            pub_date__lte=timezone.now(), choice__choice_text__isnull=False
        ).order_by('-pub_date')[:5]

But now my index looks like this:

  • What're you up to?
  • What're you up to?
  • What're you up to?
  • Whats up?
  • Whats up?

How has this happened? And how should I fix this?

(p.s. I had no idea who to use filter, so I copied code from this question. Is this why it's not worked? How does the double underscore __ notation work in filter?)

Edit: I've checked the Admin view, and there only seems to be one of each poll in the DB (I've not checked directly), but I'm confident that the way its set up I couldn't have multiple polls with the same ID (which is the case, all the 'What're you up to?' polls have ID 2, all the 'Whats up?' polls have ID 1).

解决方案

Adding distinct() (as Wolf suggests) to the chain of methods does indeed work. I think the crux of the issue is that the eventual DB query returns the combination of Polls that are recent (pub_date__lte=timezone.now()) AND polls with non-null Choices (choice__choice_text__isnull=False) regardless of overlap.

I'm not sure of a better way to prevent this at the root than just using distinct() . I tried chaining the filters and that seems to make no difference.

It turns out distinct is the correct solution:

"I did a search for the explanation, and it appears that when a query spans multiple table it can return duplicates. distinct is the proper solution here."
Ludovic Viaud in a comment

这篇关于在Django教程中过滤可选择的轮询会导致索引中的轮询复制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆