Skip to content
Browse files

Notes on iterating many-to-many

  • Loading branch information...
1 parent 7ef8ddb commit ffa98ce5dca5c55e4ae8a163d619b0a3006f71b1 @coleifer committed Aug 29, 2012
Showing with 28 additions and 0 deletions.
  1. +28 −0 docs/peewee/fields.rst
View
28 docs/peewee/fields.rst
@@ -169,6 +169,34 @@ To query what classes a given student is enrolled in:
for course in Course.filter(studentcourse_set__student__name='da vinci'):
print course.name
+To efficiently iterate over a many-to-many relation, i.e., list all students
+and their respective courses, we will query the "through" model ``StudentCourse``
+and "precompute" the Student and Course:
+
+.. code-block:: python
+
+ query = StudentCourse.select({
+ Student: ['*'],
+ StudentCourse: ['*'],
+ Course: ['*'],
+ }).join(Course).switch(StudentCourse).join(Student).order_by('name')
+
+To print a list of students and their courses you might do the following:
+
+.. code-block:: python
+
+ last = None
+ for student_course in query:
+ student = student_course.student
+ if student != last:
+ last = student
+ print 'Student: %s' % student.name
+ print ' - %s' % student_course.course.name
+
+Since we selected all fields from ``Student`` and ``Course`` in the ``select``
+clause of the query, these foreign key traversals are "free" and we've done the
+whole iteration with just 1 query.
+
.. _non_int_pks:

0 comments on commit ffa98ce

Please sign in to comment.
Something went wrong with that request. Please try again.