title: Django ORM - join a subquery
body: <p>I have to translate this query into Django ORM, but can't seem to get to it.</p>
  
  <p>I tried about every annotate/values/filter combination I could find, but I never got it to a JOIN like that (nor to correct results). I have not (yet) tried defining a custom Manager though.</p>
  
  <pre><code>SELECT
      t.id,
      t.name,
      dt.deadline
      dt.deal_id
  FROM
      dealtask dt
      JOIN task t
      ON dt.task_id = t.id
      JOIN (
          SELECT MIN(deadline) as min_deadline, task_id
          FROM dealtask 
          GROUP BY task_id
      ) dt2
      ON dt.task_id = dt2.task_id
  WHERE dt.deadline = dt2.min_deadline
  </code></pre>
  
  <p>My models are the following :</p>
  
  <pre class="lang-py prettyprint-override"><code>class DealTask(models.Model):
      deadline = models.DateTimeField(blank=True, null=True)
      deal = models.ForeignKey('Deal', on_delete=models.CASCADE)
      task = models.ForeignKey('Task', on_delete=models.CASCADE)
  
  class Task(models.Model):
      name = models.CharField(max_length=128)
  </code></pre>
  
  <p>I want to query : for every Task t, the nearest-deadline DealTask dt related to it, and the Deal related to that dt.</p>
  
  <p>Is this query possible in Django ORM ? In a constant number of queries, I mean.</p>
  
  <p>Also, if you find a simpler query that expresses the same thing, I'm all ears.</p>