270👍
Since version 1.2, Django has QuerySet.exists() method which is the most efficient:
if orgs.exists():
# Do this...
else:
# Do that...
But if you are going to evaluate QuerySet anyway it’s better to use:
if orgs:
...
For more information read QuerySet.exists() documentation.
- [Django]-Naming convention for Django URL, templates, models and views
- [Django]-Django JSONField inside ArrayField
- [Django]-Django: Display Choice Value
20👍
To check the emptiness of a queryset:
if orgs.exists():
# Do something
or you can check for a the first item in a queryset, if it doesn’t exist it will return None
:
if orgs.first():
# Do something
- [Django]-Django: manage.py does not print stack trace for errors
- [Django]-Django model one foreign key to many tables
- [Django]-Django switching, for a block of code, switch the language so translations are done in one language
18👍
If you have a huge number of objects, this can (at times) be much faster:
try:
orgs[0]
# If you get here, it exists...
except IndexError:
# Doesn't exist!
On a project I’m working on with a huge database, not orgs
is 400+ ms and orgs.count()
is 250ms. In my most common use cases (those where there are results), this technique often gets that down to under 20ms. (One case I found, it was 6.)
Could be much longer, of course, depending on how far the database has to look to find a result. Or even faster, if it finds one quickly; YMMV.
EDIT: This will often be slower than orgs.count()
if the result isn’t found, particularly if the condition you’re filtering on is a rare one; as a result, it’s particularly useful in view functions where you need to make sure the view exists or throw Http404. (Where, one would hope, people are asking for URLs that exist more often than not.)
- [Django]-Django REST Framework (DRF): Set current user id as field value
- [Django]-Using JSON in django template
- [Django]-Group by Foreign Key and show related items – Django
13👍
The most efficient way (before django 1.2) is this:
if orgs.count() == 0:
# no results
else:
# alrigh! let's continue...
- [Django]-Charts in django Web Applications
- [Django]-How do you dynamically hide form fields in Django?
- [Django]-How do you use the django-filter package with a list of parameters?
7👍
I disagree with the predicate
if not orgs:
It should be
if not orgs.count():
I was having the same issue with a fairly large result set (~150k results). The operator is not overloaded in QuerySet, so the result is actually unpacked as a list before the check is made. In my case execution time went down by three orders.
- [Django]-Django celery task: Newly created model DoesNotExist
- [Django]-Are sessions needed for python-social-auth
- [Django]-Django – How to pass several arguments to the url template tag
-2👍
You could also use this:
if(not(orgs)):
#if orgs is empty
else:
#if orgs is not empty
- [Django]-Django 1.5b1: executing django-admin.py causes "No module named settings" error
- [Django]-Exclude fields in Django admin for users other than superuser
- [Django]-Django – getting Error "Reverse for 'detail' with no arguments not found. 1 pattern(s) tried:" when using {% url "music:fav" %}