File: 02-nested-exclude.rst

package info (click to toggle)
litestar 2.19.0-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 12,500 kB
  • sloc: python: 70,169; makefile: 254; javascript: 105; sh: 60
file content (25 lines) | stat: -rw-r--r-- 1,150 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
Excluding from nested models
----------------------------

The ``exclude`` option can be used to exclude fields from models that are related to our data model by using dotted
paths. For example, ``exclude={"a.b"}`` would exclude the ``b`` attribute of an instance nested on the ``a`` attribute.

To demonstrate, let's adjust our script to add an ``Address`` model, that is related to the ``Person`` model:

.. literalinclude:: /examples/data_transfer_objects/factory/tutorial/nested_exclude.py
   :language: python
   :linenos:
   :emphasize-lines: 9-13,21,25,32,33

The ``Address`` model has three attributes, ``street``, ``city``, and ``country``, and we've added an ``address``
attribute to the ``Person`` model.

The ``ReadDTO`` class has been updated to exclude the ``street`` attribute of the nested ``Address`` model using the
dotted path syntax ``"address.street"``.

Inside the handler, we create an ``Address`` instance and assign it to the ``address`` attribute of the ``Person``.

When we call our handler, we can see that the ``street`` attribute is not included in the response:

.. image:: images/nested_exclude.png
    :align: center