[Django]-Django query: Joining two models with two fields

6👍

I got it, in part thanks to @Robert Jørgensgaard Eng

My problem was how to do the inner join using more than 1 field, in which the F object came on handly.
The correct query is:

SubjectTime.objects.filter(subject__academicrecordsubject__academic_record=record,
                           subject__academicrecordsubject__language_group=F('language_group'))

5👍

Given an AcademicRecord instance academic_record, it is either

SubjectTime.objects.filter(subject__academicrecordsubject_set__academic_record=academic_record)

or

SubjectTime.objects.filter(subject__academicrecordsubject__academic_record=academic_record)

The results reflect all the rows of the join that these ORM queries become in SQL. To avoid duplicates, just use distinct().

Now this would be much easier, if I had a django shell to test in 🙂

Leave a comment