title: Django MTPP display recursive hierarchical category in REST API
body: <p>I am trying to display my recursive category model in my Django REST API list view. 
  This is how it is now:</p>
  
  <pre><code>"results": [
      {
          "pk": 3,
          "title": "Test",
          "category": 7,
      },
      ...
  ]}
  </code></pre>
  
  <p>I want to achieve something like this:</p>
  
  <pre><code>"results": [
      {
          "pk": 3,
          "title": "Test",
          "category": [
              {
                 "id": 5,
                 "name": "Cat",
                 "slug": "cat",
                 "child": [
                     {
                         "id": 7,
                         "name": "Parser",
                         "slug": "parser",
                     }
                 ]
              }
          ],
      },
      ...
  ]}
  </code></pre>
  
  <p>I am currently using Django MPTT to create a hierarchical Model. This is the Category in models.py:</p>
  
  <pre><code>class Category(MPTTModel):
      name = models.CharField(max_length=50, unique=True)
      parent = TreeForeignKey('self', null=True, blank=True, related_name='children', db_index=True, on_delete=models.SET_NULL)
      slug = models.SlugField()
  
      class MPTTMeta:
          order_insertion_by = ['name']
  
      class Meta:
          unique_together = (('parent', 'slug',))
          verbose_name = "Category"
          verbose_name_plural = "Categories"
  
      def get_slug_list(self):
          try:
               ancestors = self.get_ancestors(include_self=True)
          except:
               ancestors = []
          else:
               ancestors = [i.slug for i in ancestors]
          slugs = []
          for i in range(len(ancestors)):
              slugs.append('/'.join(ancestors[:i + 1]))
          return slugs
  
      def __str__(self):
          return self.name
  </code></pre>
  
  <p>I am using generic views from Django Rest Framework to create the view. In views.py:</p>
  
  <pre><code>class CaseListCreateView(generics.ListCreateAPIView):
      serializer_class = CaseSerializer
  
      def get_queryset(self):
          qs = Case.objects.filter(active=True)
  
          query = self.request.GET.get("search")
          if query is not None:
              qs = qs.filter(Q(title__contains=query) | Q(text__contains=query)).distinct()
  
          return qs
  
      def perform_create(self, serializer):
          serializer.save(user=self.request.user)
  </code></pre>
  
  <p>I have tried <a href="https://github.com/heywbj/django-rest-framework-recursive" rel="nofollow noreferrer">using djangorestframework-recursive</a> to make the recursion but with no luck. </p>
  
  <p>Any help would be greatly appriciated!
  Thanks</p>