title: Django sort queryset by related model field
body: <p>I have the following models (abbreviated for clarity):</p>
  
  <pre><code>class Order(models.Model):
      some fields
  
  class OrderStatus(models.Model):
      order = models.ForiegnKey(Order)
      status = models.CharField(choices=['ORDERED', 'IN_TRANSIT', 'RECEIVED'])
      time = models.DateTimeField()
  </code></pre>
  
  <p><strong>I would like to sort all Orders that contain all three OrderStatuses by their order received time.</strong></p>
  
  <p>In other words, select the orders that have records of Ordered, In Transit, and Received like so:  </p>
  
  <pre><code>Order.objects.filter(orderstatus__status=OrderStatus.ORDERED)
               .filter(orderstatus__status=OrderStatus.IN_TRANSIT)
               .filter(orderstatus__status=OrderStatus.RECEIVED)
  </code></pre>
  
  <p>... and then sort them by the <code>time</code> field of their related <code>OrderStatus</code> model for which <code>status=OrderStatus.RECEIVED</code>. </p>
  
  <p>This is where I'm stuck. I have read the Django docs on the <code>.extra()</code> queryset modifier and direct SQL injection, but I'm still at a loss. Could I achieve it with an annotated field and <code>Q</code> objects or am I better going the <code>.extra</code> route?</p>