Why does using .latest() on an empty Django queryset return...nothing?
<p>Here's some code that builds a queryset, and prints the output at each step (for debugging):</p>

<pre><code>qs = self.get_queryset(True)
print(qs)  #   [&lt;MyModel: obj_1&gt;, &lt;MyModel: obj_2&gt;, &lt;MyModel: obj_2&gt;, &lt;MyModel: obj_3&gt;]
qs = qs.get_user(user)
print(qs)  # []
qs = qs.completed()
print(qs)  # []
qs = qs.latest('time_completed')
print(qs)  #    &lt;-- What happened?  Why is this blank?
print(qs is None) # False &lt;-- huh??
print("nothing") if not qs else print("something") #   &lt;-- blank?!?! how?!
print(type(qs)) # 
</code></pre>

<p>The last operation <code>qs.latest('time_completed')</code> prints blank, the type is blank, the if statement is ignored.  What's going on here?</p>

<p>An example where the result is NOT an empty queryset works fine (note all items are a single user, just coincidence):</p>

<pre><code>qs = self.get_queryset(True)
print(qs)  #  [&lt;MyModel: obj_1&gt;, &lt;MyModel: obj_2&gt;, &lt;MyModel: obj_2&gt;, &lt;MyModel: obj_3&gt;]
qs = qs.get_user(user)
print(qs)  #  [&lt;MyModel: obj_1&gt;, &lt;MyModel: obj_2&gt;, &lt;MyModel: obj_2&gt;, &lt;MyModel: obj_3&gt;]
qs = qs.completed()
print(qs)  #  [&lt;MyModel: obj_1&gt;, &lt;MyModel: obj_2&gt;, &lt;MyModel: obj_2&gt;, &lt;MyModel: obj_3&gt;]
qs = qs.latest('time_completed')
print(qs)  #  obj_1
print("nothing") if not qs else print("something") # something
print(type(qs)) # &lt;class 'my_app.models.MyModel'&gt;
</code></pre>

<p><a href="https://docs.djangoproject.com/en/1.10/ref/models/querysets/#latest" rel="nofollow">From the documentation</a>:</p>

<blockquote>
  <p>Like get(), earliest() and latest() raise DoesNotExist if there is no object with the given parameters.</p>
</blockquote>

<p><code>qs.latest('field')</code> should raise <code>Model.DoesNotExist</code> exception whenever <code>qs</code> is an empty queryset. </p>

<pre><code>In [2]: Entity.objects.none().latest('creation_date')
---------------------------------------------------------------------------
DoesNotExist                              Traceback (most recent call last)
</code></pre>

<p>Check the exception is not swallowed in your view/method.</p>

python django django-queryset