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.
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.
The filter() method is used to filter you search, and allows you to return only the rows that matches the search term.
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.
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.
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.
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.
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.
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.
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()
.
Just use .distinct()
at the end of your ORM.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With