282

Often I find myself wanting to get the first object from a queryset in Django, or return None if there aren't any. There are lots of ways to do this which all work. But I'm wondering which is the most performant.

qs = MyModel.objects.filter(blah = blah)
if qs.count() > 0:
    return qs[0]
else:
    return None

Does this result in two database calls? That seems wasteful. Is this any faster?

qs = MyModel.objects.filter(blah = blah)
if len(qs) > 0:
    return qs[0]
else:
    return None

Another option would be:

qs = MyModel.objects.filter(blah = blah)
try:
    return qs[0]
except IndexError:
    return None

This generates a single database call, which is good. But requires creating an exception object a lot of the time, which is a very memory-intensive thing to do when all you really need is a trivial if-test.

How can I do this with just a single database call and without churning memory with exception objects?

6
  • 31
    Rule of thumb: If you're worried about minimizing DB round-trips, don't use len() on querysets, always use .count(). Feb 25, 2011 at 23:41
  • 8
    "creating an exception object a lot of the time, which is a very memory-intensive thing" - if you're concerned about creating one extra exception, then you're doing it wrong as Python uses exceptions all over the place. Did you actually benchmarked that it's memory-intensive in your case?
    – lqc
    Jul 3, 2012 at 5:30
  • 1
    @Leopd And if you'd actually benchmarked the anwser in any way (or at least the comments), you would know it's not any faster. It actually may be slower, 'cause your creating an extra list just to throw it out. And all that is just peanuts compared to the cost of calling a python function or using Django's ORM in the first place! A single call to filter() is many, many, many times slower then raising an exception (which is still gonna be raised, 'cause that's how iterator protocol works!).
    – lqc
    Jul 5, 2012 at 22:02
  • 1
    Your intuition is correct that the performance difference is small, but your conclusion is wrong. I did run a benchmark and the accepted answer is in fact faster by a real margin. Go figure.
    – Leopd
    Jul 6, 2012 at 6:11
  • 11
    For folks using Django 1.6, they've finally added the first() and last() convenience methods: docs.djangoproject.com/en/dev/ref/models/querysets/#first
    – Wei Yen
    Mar 12, 2014 at 3:55

8 Answers 8

468

Django 1.6 (released Nov 2013) introduced the convenience methods first() and last() which swallow the resulting exception and return None if the queryset returns no objects.

5
  • 2
    it doesn't do the [:1], so it's not as fast (unless you need to evaluate the whole queryset anyway).
    – janek37
    Feb 26, 2016 at 21:16
  • 26
    also, first() and last() enforce an ORDER BY clause on a query. It will make the results deterministic but will most probably slow the query down. Jun 27, 2017 at 19:24
  • @janek37 there are no differences in performance. As indicated by cod3monk3y, it is a convenient method and it doesn't read the entire queryset.
    – Zompa
    Sep 11, 2019 at 9:55
  • 1
    @Zompa is incorrect. THERE IS A DIFFERENCE IN PERFORMANCE due to the enforced ORDER BY @Phil Krylov pointed out, which [:1] avoids. Mar 10, 2021 at 22:27
  • 1
    Rolled back edit, which added no value except rewording and took the original suggestion out of context. I am not saying that first() and last() are the fastest way, for performance, merely that these methods exist, are useful, and convenient. There is no claim that this will answer the OP's goal of peformance. But clearly I and others have found this information marginally useful.
    – cod3monk3y
    Jan 25, 2022 at 20:36
170

You can use array slicing:

Entry.objects.all()[:1].get()

Which can be used with .filter():

Entry.objects.filter()[:1].get()

You wouldn't want to first turn it into a list because that would force a full database call of all the records. Just do the above and it will only pull the first. You could even use .order_by() to ensure you get the first you want.

Be sure to add the .get() or else you will get a QuerySet back and not an object.

6
  • 15
    You would still need to wrap it in a try... except ObjectDoesNotExist, which is like the original third option but with slicing. Mar 9, 2012 at 2:55
  • 1
    What's the point of setting a LIMIT if you're gonna call get() in the end ? Let the ORM and the SQL compiler decide what's best for it's backend (for example, on Oracle Django emulates LIMIT, so it will hurt instead of helping).
    – lqc
    Jul 3, 2012 at 7:55
  • I used this answer without the trailing .get(). If a list is returned I then return the first element of the list. Jun 6, 2013 at 1:10
  • what's the different of having Entry.objects.all()[0] ??
    – James Lin
    Jun 16, 2013 at 19:59
  • 18
    @JamesLin The difference is that [:1].get() raises DoesNotExist, while [0] raises IndexError.
    – Ropez
    Sep 6, 2013 at 5:42
51

Now, in Django 1.9 you have first() method for querysets.

YourModel.objects.all().first()

This is a better way than .get() or [0] because it does not throw an exception if queryset is empty, Therafore, you don't need to check using exists()

2
  • 1
    This causes a LIMIT 1 in the SQL and I've seen claims that it can make the query slower -- although I'd like to see that substantiated: If the query only returns one item, why should the LIMIT 1 really affect performance? So I think the above answer is fine, but would love to see evidence confirming.
    – rrauenza
    Jun 29, 2018 at 16:17
  • I wouldn't say "better". It really depends on your expectations.
    – trigras
    Apr 9, 2020 at 15:12
48
r = list(qs[:1])
if r:
  return r[0]
return None
7
  • 1
    If you turn on tracing I'm pretty sure you'll even see this add LIMIT 1 to the query, and I don't know that you can do any better than this. However, internally __nonzero__ in QuerySet is implemented as try: iter(self).next() except StopIteration: return false... so it doesn't escape the exception. Feb 26, 2011 at 0:00
  • @Ben: QuerySet.__nonzero__() is never called since the QuerySet is converted to a list before checking for trueness. Other exceptions may still occur however. Feb 26, 2011 at 0:07
  • @Aron: That can generate a StopIteration exception. Apr 19, 2012 at 1:21
  • converting to list === call __iter__ to get a new iterator object and call it's next method until StopIteration is thrown. So definitively there is gonna be an exception somewhere ;)
    – lqc
    Jul 3, 2012 at 5:35
  • 14
    This answer is now outdated, take a look at @cod3monk3y answer for Django 1.6+
    – ValAyal
    Sep 26, 2014 at 14:48
8

This could work as well:

def get_first_element(MyModel):
    my_query = MyModel.objects.all()
    return my_query[:1]

if it's empty, then returns an empty list, otherwise it returns the first element inside a list.

1
  • 1
    This is by far the best solution...results in only one call to database
    – Shh
    Jul 4, 2019 at 8:29
7

If you plan to get first element often - you can extend QuerySet in this direction:

class FirstQuerySet(models.query.QuerySet):
    def first(self):
        return self[0]


class ManagerWithFirstQuery(models.Manager):
    def get_query_set(self):
        return FirstQuerySet(self.model)

Define model like this:

class MyModel(models.Model):
    objects = ManagerWithFirstQuery()

And use it like this:

 first_object = MyModel.objects.filter(x=100).first()
1
  • Call objects = ManagerWithFirstQuery as objects = ManagerWithFirstQuery() - DONT FORGET PARENTHESES - anyway, you helped me so +1
    – Kamil
    Sep 5, 2013 at 23:19
4

It can be like this

obj = model.objects.filter(id=emp_id)[0]

or

obj = model.objects.latest('id')
3

You should use django methods, like exists. Its there for you to use it.

if qs.exists():
    return qs[0]
return None
2
  • 1
    Except, if I understand so correctly, idiomatic Python typically uses an Easier to Ask for Forgiveness than Permission (EAFP) approach rather than a Look Before You Leap approach.
    – BigSmoke
    Jan 12, 2016 at 12:48
  • EAFP is not just a style recommendation, it has reasons (for example, checking before opening a file does not prevent errors). Here I think the relevant consideration is that exists + get item cause two database queries, which may be undesirable depending on the project and view.
    – merwok
    Sep 18, 2018 at 20:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.