title: Django: Filter in multiple models linked via ForeignKey?
body: <p>I'd like to create a filter-sort mixin for following values and models:</p>
  
  <pre><code>class Course(models.Model):
      title = models.CharField(max_length=70)
      description = models.TextField()
      max_students = models.IntegerField()
      min_students = models.IntegerField()
      is_live = models.BooleanField(default=False)
      is_deleted = models.BooleanField(default=False)
      teacher = models.ForeignKey(User)
  
  class Session(models.Model):
      course = models.ForeignKey(Course)
      title = models.CharField(max_length=50)
      description = models.TextField(max_length=1000, default='')
      date_from = models.DateField()
      date_to = models.DateField()
      time_from = models.TimeField()
      time_to = models.TimeField()
  
  class CourseSignup(models.Model):
      course = models.ForeignKey(Course)
      student = models.ForeignKey(User)
      enrollment_date = models.DateTimeField(auto_now=True)
  
  class TeacherRating(models.Model):
      course = models.ForeignKey(Course)
      teacher = models.ForeignKey(User)
      rated_by = models.ForeignKey(User)
      rating = models.IntegerField(default=0)
      comment = models.CharField(max_length=300, default='')
  </code></pre>
  
  <ul>
  <li>A Course could be 'Discrete mathematics 1'</li>
  <li>Session are individual classes related to a Course (e.g. 1. Introduction, 2. Chapter I, 3 Final Exam etc.) combined with a date/time</li>
  <li>CourseSignup is the "enrollment" of a student</li>
  <li>TeacherRating keeps track of a student's rating for a teacher (after course completion)</li>
  </ul>
  
  <p>I'd like to implement following functions</p>
  
  <ul>
  <li>Sort (asc, desc) by Date (earliest Session.date_from), Course.Name</li>
  <li>Filter by: Date (earliest Session.date_from and last Session.date_to), Average TeacherRating (e.g. minimum value = 3), CourseSignups (e.g. minimum 5 users signed up)
  (these options are passed via a GET parameters, e.g. sort=date_ascending&amp;f_min_date=10.10.12&amp;...)</li>
  </ul>
  
  <p><strong>How would you create a function for that?</strong></p>
  
  <p>I've tried using </p>
  
  <ul>
  <li>denormalization (just added a field to Course for the required filter/sort criterias and updated it whenever changes happened), but I'm not very satisfied with it (e.g. needs lots of update after each TeacherRating).</li>
  <li>ForeignKey Queries (Course.objects.filter(session__date_from=xxx)), but I might run into performance issues later on..</li>
  </ul>
  
  <p>Thanks for any tipp!</p>