Filter with multiple conditions on a single related object
<p><strong>EDIT: This question is a result of misunderstanding of the observed results. The snippet that was originally posted and not working solves the problem. I'm leaving it all below for anyone in the future who gets confused in the same way.</strong></p>

<p>I have two models <code>answer</code> and <code>answer_set</code>. I need to write a function that takes <code>answer_set</code> queryset and adds a filter.</p>

<p>The filter I want to apply is: <code>answer_set</code> has an <code>answer</code> with specific <code>code</code> and <code>value</code>. </p>

<p>At first I tried something like:</p>

<pre><code>q.filter(answer__code=code, answer__value=value)
</code></pre>

<p><strike>But of course that doesn't work - these are two separate answers (two joins in SQL), i.e. the filter condition is true if there is some answer with the right code and another one with the right value.</strike> I want to check if there is a single answer that satisfies both conditions. <strong>EDIT: And this code actually does exactly what I want.</strong></p>

<p>In SQL that would be very easy:</p>

<pre><code>...
JOIN answer
...
WHERE
...
answer.value = "some_value" AND answer.code = "some_code"
</code></pre>

<p>But I consider raw SQL last resort. Can it be done in Django ORM? <strong>EDIT: Yes, it does - see the snippet above.</strong></p>