title: django rest framework recursive models endpoints structure
body: <p>I have three models like this (Simplified).</p>
  <pre class="lang-py prettyprint-override"><code>class Locker(CoreModel):
      def __str__(self):
          return self.name
  
      topic = models.CharField(
          verbose_name=_('topic'),
          help_text=_(&quot;locker description&quot;),
          null=True,
          max_length=1024
      )
      name = models.CharField(
          verbose_name=_('name'),
          help_text=_(&quot;locker name&quot;),
          max_length=100,
          validators=(
              MinLengthValidator(2),
          )
      )
      owner = models.ForeignKey(
          to=settings.AUTH_USER_MODEL,
          related_name='owned_locker_set',
          verbose_name=_('owner'),
          on_delete=models.CASCADE
      )
  
  
  class Folder(CoreModel):
      def __str__(self):
          return self.name
  
      name = models.CharField(
          verbose_name=_('name'),
          help_text=_('name of the folder'),
          max_length=256
      )
      parent = models.ForeignKey(
          to='self',
          verbose_name=_('parent'),
          on_delete=models.CASCADE,
          null=True
      )
      locker = models.ForeignKey(
          to='lockers.Locker',
          verbose_name=_('locker'),
          on_delete=models.CASCADE,
          editable=False
      )
  
  
  class Upload(CoreModel):
      def __str__(self):
          return self.name
  
      name = models.CharField(
          verbose_name=_('name'),
          help_text=_(&quot;upload name&quot;),
          max_length=100,
          validators=(
              MinLengthValidator(2),
          )
      )
      locker = models.ForeignKey(
          to='lockers.Locker',
          verbose_name=_('locker'),
          on_delete=models.CASCADE,
          editable=False
      )
      parent = models.ForeignKey(
          to='lockers.Folder',
          verbose_name=_('folder'),
          on_delete=models.CASCADE,
          null=True
      )
      file = PrivateFileField(
          verbose_name=_('file'),
          help_text=_(&quot;raw file&quot;),
          upload_to='uploads/',
          storage=private_storage
      )
  </code></pre>
  <p>as you can see, the structure is recursive.
  Ideally, from the frontend, I would want to make a single request to return all the files and folders the
  locker has at the root level (parent is None). Then, I would also like to make requests to get all the files and folders inside of a specified folder ( when the user opens a folder at the frontend).</p>
  <p>I am using Django Rest Framework right now.
  However, I cannot think of a way to achieve this because I would have to serialize two models into one view (I also want support for pagination). How can this be implemented in the simplest way?</p>
  <p>Can someone please help me?
  Thanks a lot in advance!</p>
  <p>relevant github discussion is <a href="https://github.com/encode/django-rest-framework/discussions/7920" rel="nofollow noreferrer">here</a>.</p>
  <p><strong>Edit</strong>
  I can imagine the endpoints structure to be like this.</p>
  <pre class="lang-py prettyprint-override"><code># lists all files and folders in the locker.
  path('lockers/&lt;int:locker_id&gt;/metadata/')
  # lists all files and folders in the folder with the given pk.
  path('lockers/&lt;int:locker_id&gt;/folders/&lt;int:pk&gt;/metadata/'),
  # create an upload.
  path('lockers/&lt;int:locker_id&gt;/uploads/')
  # create and list the folders available.
  path('lockers/&lt;int:locker_id&gt;/folders/'),
  </code></pre>