Renaming fields#

The name of a field that is used for serialization can be changed by either explicitly declaring the new name or by declaring a renaming strategy.

Explicitly renaming fields#

We can rename fields explicitly using the rename_fields attribute. This attribute is a dictionary that maps the original field name to the new field name.

In this example, we rename the address field to location:

 1from __future__ import annotations
 2
 3from dataclasses import dataclass
 4
 5from litestar import Litestar, get
 6from litestar.dto import DataclassDTO, DTOConfig
 7from litestar.params import FromPath
 8
 9
10@dataclass
11class Address:
12    street: str
13    city: str
14    country: str
15
16
17@dataclass
18class Person:
19    name: str
20    age: int
21    email: str
22    address: Address
23    children: list[Person]
24
25
26class ReadDTO(DataclassDTO[Person]):
27    config = DTOConfig(
28        exclude={"email", "address.street", "children.0.email", "children.0.address"},
29        rename_fields={"address": "location"},
30    )
31
32
33@get("/person/{name:str}", return_dto=ReadDTO, sync_to_thread=False)
34def get_person(name: FromPath[str]) -> Person:
35    # Your logic to retrieve the person goes here
36    # For demonstration purposes, a placeholder Person instance is returned
37    address = Address(street="123 Main St", city="Cityville", country="Countryland")
38    child1 = Person(name="Child1", age=10, email="child1@example.com", address=address, children=[])
39    child2 = Person(name="Child2", age=8, email="child2@example.com", address=address, children=[])
40    return Person(
41        name=name,
42        age=30,
43        email=f"email_of_{name}@example.com",
44        address=address,
45        children=[child1, child2],
46    )
47
48
49app = Litestar(route_handlers=[get_person])

Notice how the address field is renamed to location.

../../_images/explicit_field_renaming.png

Field renaming strategies#

Instead of explicitly renaming fields, we can also use a field renaming strategy.

The field renaming strategy is specified using the rename_strategy config.

Litestar supports the following strategies:

  • lower: Converts the field name to lowercase

  • upper: Converts the field name to uppercase

  • camel: Converts the field name to camel case

  • pascal: Converts the field name to pascal case

Note

You can also define your own strategies by passing a callable that receives the field name, and returns the new field name to the rename_strategy config.

Let’s modify our example to use the upper strategy:

 1from __future__ import annotations
 2
 3from dataclasses import dataclass
 4
 5from litestar import Litestar, get
 6from litestar.dto import DataclassDTO, DTOConfig
 7from litestar.params import FromPath
 8
 9
10@dataclass
11class Address:
12    street: str
13    city: str
14    country: str
15
16
17@dataclass
18class Person:
19    name: str
20    age: int
21    email: str
22    address: Address
23    children: list[Person]
24
25
26class ReadDTO(DataclassDTO[Person]):
27    config = DTOConfig(
28        exclude={"email", "address.street", "children.0.email", "children.0.address"},
29        rename_strategy="upper",
30    )
31
32
33@get("/person/{name:str}", return_dto=ReadDTO, sync_to_thread=False)
34def get_person(name: FromPath[str]) -> Person:
35    # Your logic to retrieve the person goes here
36    # For demonstration purposes, a placeholder Person instance is returned
37    address = Address(street="123 Main St", city="Cityville", country="Countryland")
38    child1 = Person(name="Child1", age=10, email="child1@example.com", address=address, children=[])
39    child2 = Person(name="Child2", age=8, email="child2@example.com", address=address, children=[])
40    return Person(
41        name=name,
42        age=30,
43        email=f"email_of_{name}@example.com",
44        address=address,
45        children=[child1, child2],
46    )
47
48
49app = Litestar(route_handlers=[get_person])

And the result:

../../_images/field_renaming_strategy.png