Django: conditional expression : <p>I have the following models:</p>

<pre><code>class Agreement(models.Model):
    ...
    organization = models.ForeignKey("Organization")

class Signed_Agreement(models.Model):
    agreement = models.ForeignKey("Agreement")
    member = models.ForeignKey("Member")
</code></pre>

<p>What I'm trying to do is get a list of all the agreements for a particular organization (self.organization) and annotate each agreement with information about whether or not it has been signed by a particular member (self.member).</p>

<p>If the Agreement has been signed, then there exists an instance of Signed_Agreement for the particular agreement and member.</p>

<p>How do I write a query for this?</p>

<p>Here's my effort so far:</p>

<pre><code>from django.db.models import When, F, Q, Value

def get_queryset(self):

    agreements = _agreement_model.Agreement.objects.filter(
        organization=self.organization
    ).annotate(
        signed=When(Q(signed_agreement__member=self.member), then=Value(True))
    ).order_by(
        'name'
    )

    return agreements
</code></pre>

<p>This is not producing the correct results. </p>

<p>Any help would be appreciated. Thanks in advance.</p>
