File: 07-read-only-fields.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 (32 lines) | stat: -rw-r--r-- 1,634 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
26
27
28
29
30
31
32
.. _read-only-fields:

Read only fields
----------------

Sometimes, fields should never be able to be specified by the client. For example, upon creation of a new resource
instance, the ``id`` field of a model should be generated by the server and not specified by the client.

.. literalinclude:: /examples/data_transfer_objects/factory/tutorial/read_only_fields_error.py
   :language: python
   :linenos:
   :emphasize-lines: 14

In this adjustment, we add an ``id`` field to the ``Person`` model. We also create a new class, ``WriteDTO`` that is
instructed to ignore the ``id`` attribute.

The ``WriteDTO`` class is assigned to the handler via the ``dto`` kwarg (``dto=WriteDTO``) meaning that the ``id`` field
will be ignored from any data received from the client when creating a new ``Person`` instance.

When we try to create a new ``Person`` instance with an ``id`` field specified, we get an error:

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

What's happening? The DTO is trying to construct an instance of the ``Person`` model but we have excluded the ``id``
field from the accepted client data. The ``id`` field is required by the ``Person`` model, so the model constructor
raises an error.

There is more than one way to address this issue, for example we could assign a default value to the ``id`` field and
overwrite the default in the handler, or we could create an entirely separate model that has no ``id`` field, and
transfer the data from that to the ``Person`` model in the handler. However, Litestar has a built-in solution for this:
:class:`DTOData <litestar.dto.data_structures.DTOData>`.