Debian Codegraph @

docs/tutorials/dto-tutorial/04-max-nested-depth.rst

raw
1 Max nested depth
2 ----------------
3
4 As we saw in the previous section, even though we didn't explicitly exclude the ``children`` from the nested
5 ``Person.children`` representations, they were not included in the response.
6
7 Here's a reminder of the output:
8
9 .. image:: images/nested_collection_exclude.png
10 :align: center
11
12 Given that we didn't explicitly exclude it from the response, each of the ``Person`` objects in the ``children``
13 collection should have an empty ``children`` collection. The reason they do not is due to
14 :attr:`max_nested_depth <litestar.dto.config.DTOConfig.max_nested_depth>` and its default value of ``1``.
15
16 The ``max_nested_depth`` attribute is used to limit the depth of nested objects that are
17 included in the response. In this case, the ``Person`` object has a ``children`` collection, which is a collection of
18 nested ``Person`` objects, so this represents a nested depth of 1. The ``children`` collections of the items in the
19 ``Person.children`` collection are at a 2nd level of nesting, and so are excluded due to the default value of
20 ``max_nested_depth``.
21
22 Let's now modify our script to include the children of children in the response:
23
24 .. literalinclude:: /examples/data_transfer_objects/factory/tutorial/max_nested_depth.py
25 :language: python
26 :linenos:
27 :emphasize-lines: 28
28
29 We now see those empty collections in our output:
30
31 .. image:: images/max_nested_depth.png
32 :align: center
33
34 Now that we've seen how to use the ``max_nested_depth`` configuration, we'll revert to using the default value of ``1``
35 for the remainder of this tutorial.