title: How to get latest and earliest from a django QuerySet
body: <p>In my django app, there are a few different ways that I get a QuerySet object of the same model.</p>
  
  <p>I would like to use one function to render the data -- and this has been working fine, until I decided to add both <strong>earliest</strong> and <strong>latest</strong> time to display on top of the list.</p>
  
  <p>Here's my latest attempt:</p>
  
  <pre><code>def render_list(request, objList):
      latest = earliest = None
      if objList:
          latest = objList.latest('time').time
          earliest = objList.earliest('time').time
  
      context = {'objList': objList,
                 'earliest': earliest,
                 'latest': latest,
                 }
      return render(request, 'list.html', context)
  </code></pre>
  
  <p>This produces the error message:</p>
  
  <pre><code>TypeError: Cannot reverse a query once a slice has been taken.
  </code></pre>
  
  <p>Is there a way to get around this problem?</p>
  
  <p><strong>More details:</strong>
  The function <code>render_list</code> is being called by multiple view functions in <code>views.py</code>, for example :</p>
  
  <pre><code>def list_all(request):
      objList = MyTable.objects.order_by('ip')
      return render_list(request,objList)
  
  def detail(request, ip, group):
      objList = MyTable.objects
      try:
          item = MyTable.objects.get(ip=ip)
          # display the details page...
  
      except MyTable.DoesNotExist:
          objList = MyTable.objects.complex_filter(...)
          # more code
          # ...
          return render_list(request, objList)
  </code></pre>
  
  <p>and corresponding lines in <code>urls.py</code></p>
  
  <pre><code>path('list', views.list_all)
  path('&lt;group&gt;/&lt;ip&gt;', views.detail)
  </code></pre>