[Answer]-Making queries that involves ForeignKey in Django

1👍

You’re not going to be able to do this from a Student queryset. The only way you will end up with Adult objects from a Student class is from a Student instance via its reverse-related-accessor: student_instance.relationships.all().

The missing ingredient in your django ORM travels is probably the fact that you can query related objects (FK, OneToOne) via its related_name (or by default, the model name).

student_qs = Student.objects.all() # some query
adults_in_us = Adult.objects.filter(parent__in=student_qs, profile__country='US')

Leave a comment