Django filter based on specific item from reverse relation
<p>I have two models that could look something like this:  </p>

<pre><code>Course(models.Model):
  pass

WeeklyPrice(models.Model):
  num_weeks = models.IntegerField()
  price = models.DecimalField()
  course = models.ForeignKey(Course)
  is_erased = models.BooleanField()
</code></pre>

<p>The thing is i have a search form, which needs to be able to filter by minimum number of weeks.</p>

<p>On Course creation, a bunch of <code>WeeklyPrices</code> are also created, which can then be deleted (logically by setting is_erased to True), so a good example would be that Course has 13 instances of WeeklyPrice, starting at week 4, because weeks 1, 2 and 3 have been deleted.</p>

<p>So how can i retrieve a queryset of <code>Courses</code> with an instance of <code>WeeklyPrice</code> whose <code>num_weeks</code> is less or equal to the <code>duration</code> variable retrieved from the search form?</p>

<p>Ok, so i ended up finding out it's a lot simpler than it sounds...</p>

<p>The filter needed is:  </p>

<pre><code>courses.filter(weekly_prices__is_erased=False, weekly_prices__num_weeks=duration)
</code></pre>

python django