-
I'd like to reuse a field definition, and sometimes a field gets used in a list. See below:
Unfortunately this does not work. The line What is the correct way to do this? |
Beta Was this translation helpful? Give feedback.
Answered by
uriyyo
Mar 27, 2024
Replies: 1 comment
-
Hi @Wyko, If you are using pydantic v2 you can use from typing import Annotated, TypeAlias
from pydantic import Field, BaseModel
ConfigField: TypeAlias = Annotated[
dict,
Field(
default_factory=dict,
title="The configuration.",
description="A dictionary containing the configuration.",
)
]
class Config(BaseModel):
config: ConfigField
class MultiConfig(BaseModel):
configs: list[ConfigField]
multi_config = MultiConfig(
configs=[
{"key": "value"},
{"key": "value"},
]
)
print(multi_config) |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
Wyko
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi @Wyko,
If you are using pydantic v2 you can use
Annotated
to addField
to specific type: