title: Django queryset exclude() with multiple related field clauses
body: <p>I am creating a sparse preferences table in Django.  My model is simple:</p>
  
  <pre><code>class Preference(models.Model):
      user = models.ForeignKey(settings.AUTH_USER_MODEL, related_name='preferences')
      preference = models.CharField(max_length=255, db_index=True)
      value = models.BooleanField()
  </code></pre>
  
  <p>Some preferences have default states, so I need to be able to ask the database two questions: "Which users have this preference set to a certain value?" and "Which users do not have this preference set to that value (either because they do not have the preference set or because they have proactively set the preference to another value)?"</p>
  
  <p>My problem is that the former question works, but the latter question (the same query clauses, but with an <code>exclude()</code> instead of a <code>filter()</code>) does not work.  For instance:</p>
  
  <p>My test database has 14 users, and a single user has two preferences set: <code>'PREF_A'</code> is set to <code>True</code> and <code>'PREF_B'</code> is set to <code>False</code>.</p>
  
  <pre><code>&gt;&gt;&gt; User.objects.all().count()
  14
  &gt;&gt;&gt; User.objects.filter(preferences__preference="PREF_A", preferences__value=True).count()
  1
  &gt;&gt;&gt; User.objects.exclude(preferences__preference="PREF_A", preferences__value=True).count()
  13
  &gt;&gt;&gt; User.objects.filter(preferences__preference="PREF_A", preferences__value=False).count()
  0
  &gt;&gt;&gt; User.objects.exclude(preferences__preference="PREF_A", preferences__value=False).count()
  13
  </code></pre>
  
  <p>So, my results say that:</p>
  
  <p>There are 14 users in total</p>
  
  <ul>
  <li><p>1 user has PREF_A set to True</p></li>
  <li><p>13 users do not have PREF_A set to True</p></li>
  <li><p>0 users have PREF_A set to False</p></li>
  <li><p>13 users do not have PREF_A set to False  <strong>&lt;--- this is inaccurate</strong></p></li>
  </ul>
  
  <p>Where is this query going wrong, and how can I write the query to properly exclude people who have a specific preference set to a specific value?</p>
  
  <p>I have tried using <code>Q</code> and <code>~Q</code> to see if the behavior would be different, but the results were the same.</p>