title: Django: get parent objects matching condition on child
body: <p>Couldn't think of a more appropriate question title, but I'm looking for some advice on how to implement the following requirement:</p>
  
  <p>I have a <code>Project</code> class, which may contain <code>Task</code> objects. Tasks have an assignee. In my Django template, I'd like to render a 'tree' of projects and tasks for a given user, showing only those projects that have at least one task assigned to the user, and within each project, only those tasks assigned to the user.</p>
  
  <p>My initial approach was to first find projects with at least one assigned task:</p>
  
  <pre><code>def list_assigned_tasks(request, assignee_id):
      projects = Project.objects.filter(task__assignee=assignee_id).distinct()
      # ...
  </code></pre>
  
  <p>(Not the most efficient approach, but I'm only dealing with small numbers of projects &amp; tasks.)</p>
  
  <p>Then I thought I might write a method on <code>Project</code> to retrieve only those tasks assigned to a user:</p>
  
  <pre><code>class Project(models.Model):
      # ...
      def assigned_tasks(self, assignee_id):
          return self.task_set.filter(assignee=assignee_id)
  </code></pre>
  
  <p>or something like that.</p>
  
  <p>However, I wouldn't be able to call that method directly from within a template. So, my question is: is there a better way of achieving this? Looking for advice on the QuerySet and template implementation.</p>
  
  <p>(My other thought was to just bolt the assigned tasks onto each project instance in <code>list_assigned_tasks</code> - is that a valid approach?)</p>