1

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 1

4

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.