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

And the result:

../../_images/field_renaming_strategy.png