title: In djangorestframewokr how to apply group by in django serializer using queryset?
body: <p><strong>models.py</strong></p>
  <p>Sharing two mapping table where in <strong>CategoryLessonMapping</strong>, <strong>category</strong> is mapped with <strong>lesson</strong> and in  <strong>UserLessonMapping</strong>, <strong>user</strong> is mapped with <strong>lesson</strong></p>
  <pre><code>------- category mapped with lesson --------------------
  class CategoryLessonMapping(models.Model):
      category = models.ForeignKey(MasterCategory, on_delete=models.CASCADE, blank=True, null=True)
      lesson = models.ForeignKey(Lesson, on_delete=models.CASCADE, blank=True, null=True)
      
      class Meta:
          db_table = 'category_lesson_mapping'
          
  
  ------- user mapped with lesson --------------------
  class UserLessonMapping(models.Model):
      user = models.ForeignKey(User, on_delete=models.CASCADE, blank=True, null=True)
      lesson = models.ForeignKey(Lesson, on_delete=models.CASCADE, blank=True, null=True)
      
      class Meta:
          db_table = 'user_lesson_mapping'
          
  </code></pre>
  <p><strong>views.py</strong></p>
  <pre><code>
  ------- getting lesson and category related to lesson in json format --------------------
  class PatientLessonListView(ListAPIView):
      serializer_class = PatientLessonSerializer
      queryset = UserLessonMapping.objects.all()
      permission_class = (permissions.IsAuthenticated,)
      
      def get_queryset(self):
          
          user_lesson = self.queryset.filter(user=self.request.user)
          user_lesson_id = user_lesson.values_list(&quot;lesson_id&quot;, flat=True)
          return CategoryLessonMapping.objects.filter(lesson_id__in=user_lesson_id)
  </code></pre>
  <p><strong>serializers.py</strong></p>
  <pre><code>
  --------- get lesson serialized data -------------
  class LessonSerializer(serializers.ModelSerializer):
      
      class Meta:
        model = Lesson
        fields = ('id', 'lesson_type', 'content', 'description')
        depth = 1
        
  --------- get category serialized data -------------
  class CategorySerializer(serializers.ModelSerializer):
      class Meta:
        model = MasterCategory
        fields = ('id', 'name', 'code')
  
  
  --------------- getting lesson and category related to user ----------used in views.py------------
  class PatientLessonSerializer(serializers.ModelSerializer):
      
      lesson = LessonSerializer()
      category = CategorySerializer()    
      
          
      class Meta:
          model = CategoryLessonMapping
          fields = ['lesson','category']
  
  </code></pre>
  <p>then json format in which i'm getting it is</p>
  <pre><code>[
      {
          &quot;lesson&quot;: {
              &quot;id&quot;: 1,
              &quot;lesson_type&quot;: {
                  &quot;id&quot;: 1,
                  &quot;lesson_type&quot;: &quot;Video&quot;,
                  &quot;lesson_code&quot;: &quot;VIDEO&quot;,
                  &quot;is_active&quot;: 1,
                  &quot;created_at&quot;: &quot;2022-07-14T13:36:16Z&quot;,
                  &quot;updated_at&quot;: &quot;2022-07-14T13:36:16Z&quot;
              },
              &quot;content&quot;: &quot;https://www.youtube.com/watch?v=inpok4MKVLM&quot;,
              &quot;description&quot;: &quot;Please follow the below steps for next few days&quot;
          },
          &quot;category&quot;: {
              &quot;id&quot;: 1,
              &quot;name&quot;: &quot;insomnia&quot;,
              &quot;code&quot;: &quot;INS&quot;
          }
      }
  ]
  </code></pre>
  <p>where in an object i'm getting 2 key-value one is 'category' and other is 'lesson' but what actually i want is grouping all the lessons related to one category, inside category key. like this</p>
  <pre><code>{
    &quot;categories&quot;: [
      {
        &quot;name&quot;: &quot;insomnia&quot;,
        &quot;code&quot;: &quot;INS&quot;,
        &quot;image&quot;: &quot;https://images.everydayhealth.com/images/emotional-health/meditation/a-complete-guide-to-meditation-722x406.jpg?w=1110&quot;,
        &quot;icon&quot;: &quot;moon&quot;,
        &quot;lessions&quot;: [
          {
            &quot;type&quot;: &quot;video&quot;,
            &quot;link&quot;: &quot;https://www.youtube.com/watch?v=inpok4MKVLM&quot;,
            &quot;description&quot;: &quot;Please follow the below steps for next few days&quot;
          }
        ]
      }
    ]
  }
  </code></pre>