Filtering out choiceless polls in the Django tutorial causes polls in the index to duplicate

被刻印的时光 ゝ 提交于 2019-12-06 17:27:57
Pureferret

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!