Debian Codegraph @

docs/tutorials/dto-tutorial/02-nested-exclude.rst

raw
1 Excluding from nested models
2 ----------------------------
3
4 The ``exclude`` option can be used to exclude fields from models that are related to our data model by using dotted
5 paths. For example, ``exclude={"a.b"}`` would exclude the ``b`` attribute of an instance nested on the ``a`` attribute.
6
7 To demonstrate, let's adjust our script to add an ``Address`` model, that is related to the ``Person`` model:
8
9 .. literalinclude:: /examples/data_transfer_objects/factory/tutorial/nested_exclude.py
10 :language: python
11 :linenos:
12 :emphasize-lines: 9-13,21,25,32,33
13
14 The ``Address`` model has three attributes, ``street``, ``city``, and ``country``, and we've added an ``address``
15 attribute to the ``Person`` model.
16
17 The ``ReadDTO`` class has been updated to exclude the ``street`` attribute of the nested ``Address`` model using the
18 dotted path syntax ``"address.street"``.
19
20 Inside the handler, we create an ``Address`` instance and assign it to the ``address`` attribute of the ``Person``.
21
22 When we call our handler, we can see that the ``street`` attribute is not included in the response:
23
24 .. image:: images/nested_exclude.png
25 :align: center