I have a pydantic model that has an instance of another model as one of its attributes.
For my application, I need that model to be written out in a custom way. I thought I could do this by setting json_encoders in the model Config but I can't get it working. I can get it to work for basic types eg. datetime but not with my own class.
See minimal example below:
from pydantic import BaseModel
def write_list_item(item):
return [item.a, item.b]
class ListItem(BaseModel):
a: str
b: str
class ToyList(BaseModel):
list_item: ListItem
class Config:
json_encoders = {ListItem: write_list_item}
tl = ToyList(list_item=ListItem(a="a1", b="b2"))
print(tl.json())
Expected output:
["a1, "b1"]
Observed output:
{"a": "a1", "b": "b1"}
Can anyone spot what i'm doing wrong?