title: Django GROUP BY ForeignKey query
body: <p>This is my models.py:</p>
  
  <pre><code>class QuestionCategory(models.Model):
      name = models.CharField(max_length=200)
  
  class Question(models.Model):
      question = models.CharField(max_length=200)
      answer = models.TextField()
      category = models.ForeignKey(QuestionCategory)
  </code></pre>
  
  <p>What I want to do is to get the questions and <code>group_by</code> the <code>QuestionCategory</code></p>
  
  <p>So I can display my questions like this:</p>
  
  <ul>
  <li><p>Question_Category (1)</p>
  
  <ul>
  <li><p>Question (1)</p>
  
  <ul>
  <li>Answer (1)</li>
  </ul></li>
  <li><p>Question (2)</p>
  
  <ul>
  <li>Answer (2)</li>
  </ul></li>
  </ul>
  
  <p>....</p></li>
  <li><p>Question_Category (2)</p>
  
  <ul>
  <li><p>Question (3)</p>
  
  <ul>
  <li>Answer (3)</li>
  </ul></li>
  <li><p>Question (...)</p>
  
  <ul>
  <li>Answer (...)</li>
  </ul></li>
  </ul>
  
  <p>...</p></li>
  </ul>
  
  <p>This is the doc to my question: <a href="https://docs.djangoproject.com/en/1.8/topics/db/aggregation/" rel="nofollow">https://docs.djangoproject.com/en/1.8/topics/db/aggregation/</a></p>