Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why .filter() in django returns duplicated objects?

Tags:

django

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:

class IndexView(generic.ListView):
    ...
    def get_queryset(self):
        return Question.objects.filter(
            pub_date__lte=timezone.now(),
            choice__isnull=False
        ).order_by('-pub_date')[:5]

But this returned two objects which are exactly same.

I think choice__isnull=False caused the problem, but not sure.

like image 572
Hyunan Kwon Avatar asked Jul 19 '16 07:07

Hyunan Kwon


People also ask

What does Django-filter return?

Django provides a filter() method which returns a subset of data. It accepts field names as keyword arguments and returns a QuerySet object. As database has only one record where name is 'tom' , the QuerySet object contains only a single record.

What is the purpose of filter () method in Django?

The filter() method is used to filter you search, and allows you to return only the rows that matches the search term.

What is the difference between filter and get method in Django?

Basically use get() when you want to get a single unique object, and filter() when you want to get all objects that match your lookup parameters.

What does objects all () return in Django?

all() Returns a copy of the current QuerySet (or QuerySet subclass). This can be useful in situations where you might want to pass in either a model manager or a QuerySet and do further filtering on the result.

What is the contains filter in Django?

The contains filter in Django returns all the objects that carry case-sensitive strings in the given field. Let’s understand its usage with the help of an example and we will use the Person model in the example. As you can see in the model data, there are multiple records with the same first name but a different last name.

How to filter objects in Django based upon the “not equal” condition?

In this section, we will understand how to filter objects in Django based upon the “ Not Equal ” condition. This simply means that we have to filter all the data that does not match the given condition. For this execution, we will use the exclude () method with the QuerySet.

How to exclude data from a queryset in Django?

This simply means that we have to filter all the data that does not match the given condition. For this execution, we will use the exclude () method with the QuerySet. The exclude () method in Django returns a new QuerySet with the objects that do not match the given parameter. Let’s understand its usage by executing an example.

How to use the LIKE operator in Django filter?

But in Django, we cannot directly use the LIKE operator instead we use the contains field lookup in the filter method to search the given pattern. The example related to contains field lookup is given in the previous section. To confirm whether the contains option is equivalent to the LIKE operator, we will run the following given code.


2 Answers

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().

like image 189
Vladimir Danilov Avatar answered Sep 25 '22 19:09

Vladimir Danilov


Just use .distinct() at the end of your ORM.

like image 24
Deepanshu Mehta Avatar answered Sep 24 '22 19:09

Deepanshu Mehta