Say I have a user id, and the users table also has a field "teacher id". This points to the teachers table, and the teacher table in turn also has a field called "substitute id" (This is not really what my fields represent, but I'm just using an example.) How, in Django, would I get all the substitute id's for all teachers who are teachers for a given user id? I know how to do it in SQL, but not really sure how the Django version is.
1 Answer
Here's one way to do it:
Teacher.objects.filter(user__id=some_id).values_list('substitute_id', flat=True)
The key is that foreign key relationships can be reversed -- you can make Teacher queries that are predicated on User.