title: Django: Return all nested Sub Model from a given Model (which isn't directly linked to given model)
body: <p>I have my models.py as shown below:</p>
  <pre><code>from django.db import models
  
  # Create your models here.
  class Category(models.Model):
      categoryType = models.CharField(max_length=100,blank = False, unique = True, null = False)
  
      def __str__(self):
          return self.categoryType
  
  class SubCategory(models.Model):
      subcategoryType = models.CharField(max_length=100)
      categoryType = models.ForeignKey(Category,on_delete=models.CASCADE, null = True, related_name='category_type')
  
      def __str__(self):
          return f'{self.categoryType} :: {self.subcategoryType}'
      
  class Product(models.Model):
      productName = models.CharField(max_length=50,blank = False, null = False)
      subCategoryType = models.ForeignKey(SubCategory,on_delete=models.SET_NULL, null=True,related_name='product_subcategories')
      #categoryType = models.ForeignKey(Category,on_delete=models.SET_NULL, null=True,related_name='product_categories')
  
      def __str__(self):
          return f'{self.productName} : {self.subcategoryType}'
  </code></pre>
  <p>I have created a serializer to get all products within a given category as shown below:</p>
  <pre><code>class ProductSerializerSpecific(serializers.ModelSerializer):
      class Meta:
          model = Product
          fields = ('id','productName')
  class SubCategoryProductsSpecific(serializers.ModelSerializer):
      products = ProductSerializerSpecific(source='product_subcategories', many=True)
      class Meta:
          model = SubCategory
          fields = ['products']
  
  class CategoryProducts(serializers.ModelSerializer):
      products = SubCategoryProductsSpecific(source='category_type', many=True)
      class Meta:
          model = Category
          fields = ('id','products')
  </code></pre>
  <p>My View goes like this:</p>
  <pre><code>class ListAllCategoryProducts(viewsets.ReadOnlyModelViewSet):
      queryset = Category.objects.all()
      serializer_class = CategoryProducts
  </code></pre>
  <p>And finally I registered by route like this:</p>
  <pre><code>router.register(r&quot;category_products&quot;, views.ListAllCategoryProducts, basename=&quot;categories_products&quot;)
  </code></pre>
  <p>When I do a GET request to get all products with a given ID as shown below:</p>
  <pre><code>GET http://localhost:8000/category_products/1 HTTP/1.1
  </code></pre>
  <p>The output comes as shown below:</p>
  <pre><code>{
    &quot;id&quot;: 1,
    &quot;products&quot;: [
      {
        &quot;products&quot;: []
      },
      {
        &quot;products&quot;: []
      },
      {
        &quot;products&quot;: []
      },
      {
        &quot;products&quot;: [
          {
            &quot;id&quot;: 1,
            &quot;productName&quot;: &quot;Dell XPS&quot;
          },
          {
            &quot;id&quot;: 2,
            &quot;productName&quot;: &quot;Macbook Pro&quot;
          },
          {
            &quot;id&quot;: 3,
            &quot;productName&quot;: &quot;Dell Inspiron&quot;
          },
          {
            &quot;id&quot;: 4,
            &quot;productName&quot;: &quot;Lenevo Ideapad&quot;
          },
          {
            &quot;id&quot;: 5,
            &quot;productName&quot;: &quot;Asus&quot;
          }
        ]
      }
    ]
  }
  </code></pre>
  <p>Where each dictionary inside list represents the subcategory, but I was interested in getting a result which had just products which would look something like this:</p>
  <pre><code>{
    &quot;id&quot;: 1,
    &quot;products&quot;: [
        {
            &quot;id&quot;: 1,
            &quot;productName&quot;: &quot;Dell XPS&quot;
          },
          {
            &quot;id&quot;: 2,
            &quot;productName&quot;: &quot;Macbook Pro&quot;
          },
          {
            &quot;id&quot;: 3,
            &quot;productName&quot;: &quot;Dell Inspiron&quot;
          },
          {
            &quot;id&quot;: 4,
            &quot;productName&quot;: &quot;Lenevo Ideapad&quot;
          },
          {
            &quot;id&quot;: 5,
            &quot;productName&quot;: &quot;Asus&quot;
          }
    ]
  }
  </code></pre>
  <p>If you observe carefully the result I am getting basically is category =&gt; subcategory =&gt; products, whereas I am interested in category =&gt; products (where subcategory is fetched from category and products are fetched from subcategory). What should be the way to do the same? Since products aren't directly linked with category rather they need to go via sub category.</p>