Why .filter() in django returns duplicated objects?

后端 未结 3 601
悲&欢浪女
悲&欢浪女 2021-01-17 12:03

I\'ve followed django tutorial and arrived at tutorial05.

I tried to not show empty poll as tutorial says, so I added filter condition like this:

cla         


        
3条回答
  •  梦毁少年i
    2021-01-17 12:39

    choice__isnull causes the problem. It leads to join with choice table (to weed out questions without choices), that is something like this:

    SELECT question.*
      FROM question
      JOIN choice
        ON question.id = choice.question_id
     WHERE question.pub_date < NOW()
    

    You can inspect query attribute of QuerySet to be sure. So if you have one question with two choices, you will get that question two times. You need to use distinct() method in this case: queryset.distinct().

提交回复
热议问题