title: limit n results per group by - Django queryset
body: <p>I am trying to create a query where I can pull the last 3 forum posts per category that is shown on the screen. Here is the way my model is setup:</p>
  
  <pre><code>class ForumCategory(models.Model):
      name = models.CharField(max_length=40)
      description = models.TextField()
      class_id = models.ForeignKey(Class)
  
  
  class ForumPost(models.Model):
      name = models.CharField(max_length=100)
      details = models.TextField()
      author = models.ForeignKey(User)
      class_id = models.ForeignKey(Class)
      category = models.ForeignKey(ForumCategory)
      created_at = models.DateTimeField(auto_now_add=True)
  </code></pre>
  
  <p>My current query returns the 3 most recent posts in general and not 3 per category:</p>
  
  <pre><code>categories = ForumCategory.objects.filter(class_id=class_id)
          forum_post = ForumPost.objects.filter(category_id__in=categories.values_list('id', flat=True)).order_by('-category_id')[:3]
  </code></pre>
  
  <p>So I first grab all categories that belong to a specific class and I want to list those categories along with the 3 most recent posts in each category.</p>