title: Fastest way to get the first object from a queryset in django?
body: <p>Often I find myself wanting to get the first object from a queryset in Django, or return <code>None</code> 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.</p>
  
  <pre><code>qs = MyModel.objects.filter(blah = blah)
  if qs.count() &gt; 0:
      return qs[0]
  else:
      return None
  </code></pre>
  
  <p>Does this result in two database calls?  That seems wasteful.  Is this any faster? </p>
  
  <pre><code>qs = MyModel.objects.filter(blah = blah)
  if len(qs) &gt; 0:
      return qs[0]
  else:
      return None
  </code></pre>
  
  <p>Another option would be:</p>
  
  <pre><code>qs = MyModel.objects.filter(blah = blah)
  try:
      return qs[0]
  except IndexError:
      return None
  </code></pre>
  
  <p>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.</p>
  
  <p>How can I do this with just a single database call and without churning memory with exception objects?</p>