[Fixed]-Filter on through table in Django

1👍

Your starting point is a TeamMember, a teammember has reverse relation to Owner which you need to use in order to filter on Owner.report and Owner.primary_owner bit.

e.g.

owners = TeamMember.objects.filter(
    owner__report_id=id, #its also possible to use: owner__report__id
    owner__primary_owner=1 #I have no idea why you use contains on an IntegerField?
).distinct()[:1]
👤Todor

0👍

This should work:

primary_owner = TeamMember.objects.filter(owner__report__id=id,
                                          owner__primary_owner=1).first()

The model name Owner is a bit missleading, what about ReportOwnership (or suggest any other!). Also, primary_owner should be a BooleanField

Leave a comment