37๐
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()
.
- [Django]-Unknown format code 'f' for object of type 'unicode'
- [Django]-Generating a non-sequential ID/PK for a Django Model
- [Django]-Django prefetch_related with limit
3๐
A little late to the party, but I figured it could help others looking up the same issue.
Instead of using choice__isnull=False
with the filter()
method, use it with exclude()
instead to exclude out any questions without any choices. So your code would look something like this:
...
def get_queryset(self):
return Question.objects.filter(pub_date__lte=timezone.now()).exclude(choice__isnull=True).order_by('-pub_date')[:5]
By doing it this way, it will return only one instance of the question
. Be sure to use choice_isnull=True
though.
- [Django]-How to use 2 different cache backends in Django?
- [Django]-Django accessing ForeignKey model objects
- [Django]-How to save output from django call_command to a variable or file
-4๐
Because you created two objects with same properties. If you want to ensure uniqueness, you should add validation in clean
and add unique index on identifier field too.
Besides filter
returns all the objects that match the criteria, if you are expecting only one item to be returned, you should use get
instead. get
would raise exception if less or more than 1 item is found.
- [Django]-Django 1.9.2 AssertionError: database connection isn't set to UTC
- [Django]-Command "django-admin.py startproject mysite" not recognized
- [Django]-Django: "Reverse not found"