title: Django filter on two fields of the same foreign key object
body: <p>I have a database schema similar to this:</p>
  
  <pre><code>class User(models.Model):
      … (Some fields irrelevant for this query)
  
  class UserNotifiy(models.Model):
      user = models.ForeignKey(User)
      target = models.ForeignKey(&lt;Some other Model&gt;)
      notification_level = models.SmallPositivIntegerField(choices=(1,2,3))
  </code></pre>
  
  <p>Now I want to query for all Users that have a UserNotify object for a specific target and at least a specific notification level (e.g. 2).</p>
  
  <p>If I do something like this:</p>
  
  <pre><code>User.objects.filter(usernotify__target=desired_target,
      usernotify__notification_level__gte=2)
  </code></pre>
  
  <p>I get all Users that have a UserNotify object for the specified target and at least one UserNotify object with a notification_level greater or equal to 2. These two UserNotify objects, however, do not have to be identical.</p>
  
  <p>I am aware that I can do something like this:</p>
  
  <pre><code>user_ids = UserNotify.objects.filter(target=desired_target,
      notification_level__gte=2).values_list('user_id', flat=True)
  users = User.objects.filter(id__in=user_ids).distinct()
  </code></pre>
  
  <p>But this seems a step too much for me and I believe it executes two queries.</p>
  
  <p>Is there a way to solve my problem with a single query?</p>