ModelField Class — fastapi Architecture
Architecture documentation for the ModelField class in v2.py from the fastapi codebase.
Entity Profile
Dependency Diagram
graph TD 413233b4_cb0b_062e_c20b_4dfc7c774216["ModelField"] 3e134d50_38c1_8523_f518_6686c1d9752b["v2.py"] 413233b4_cb0b_062e_c20b_4dfc7c774216 -->|defined in| 3e134d50_38c1_8523_f518_6686c1d9752b c65bd749_7b67_262f_bdad_b8eb93afadd4["alias()"] 413233b4_cb0b_062e_c20b_4dfc7c774216 -->|method| c65bd749_7b67_262f_bdad_b8eb93afadd4 11c0c241_0333_455d_5cc9_5d65c162b2f2["validation_alias()"] 413233b4_cb0b_062e_c20b_4dfc7c774216 -->|method| 11c0c241_0333_455d_5cc9_5d65c162b2f2 dfe8a673_7a98_7ccd_8f6b_d98475000f53["serialization_alias()"] 413233b4_cb0b_062e_c20b_4dfc7c774216 -->|method| dfe8a673_7a98_7ccd_8f6b_d98475000f53 dbdd4ac4_3b4f_8da7_edc8_c9b5ce6592f4["default()"] 413233b4_cb0b_062e_c20b_4dfc7c774216 -->|method| dbdd4ac4_3b4f_8da7_edc8_c9b5ce6592f4 eba6f1ce_1c5f_6206_5659_da76ae8122e0["__post_init__()"] 413233b4_cb0b_062e_c20b_4dfc7c774216 -->|method| eba6f1ce_1c5f_6206_5659_da76ae8122e0 bbf9c94a_b570_f8a6_a965_c5290eae408a["get_default()"] 413233b4_cb0b_062e_c20b_4dfc7c774216 -->|method| bbf9c94a_b570_f8a6_a965_c5290eae408a 33f41d4d_1eb8_4336_8d89_8c96f8052913["validate()"] 413233b4_cb0b_062e_c20b_4dfc7c774216 -->|method| 33f41d4d_1eb8_4336_8d89_8c96f8052913 1dc99bde_e086_531f_f13c_0f42087dd407["serialize()"] 413233b4_cb0b_062e_c20b_4dfc7c774216 -->|method| 1dc99bde_e086_531f_f13c_0f42087dd407 e30e8197_62d2_8891_e7f5_9ad5b0f91d9d["__hash__()"] 413233b4_cb0b_062e_c20b_4dfc7c774216 -->|method| e30e8197_62d2_8891_e7f5_9ad5b0f91d9d
Relationship Graph
Source Code
fastapi/_compat/v2.py lines 82–186
class ModelField:
field_info: FieldInfo
name: str
mode: Literal["validation", "serialization"] = "validation"
config: Union[ConfigDict, None] = None
@property
def alias(self) -> str:
a = self.field_info.alias
return a if a is not None else self.name
@property
def validation_alias(self) -> Union[str, None]:
va = self.field_info.validation_alias
if isinstance(va, str) and va:
return va
return None
@property
def serialization_alias(self) -> Union[str, None]:
sa = self.field_info.serialization_alias
return sa or None
@property
def default(self) -> Any:
return self.get_default()
def __post_init__(self) -> None:
with warnings.catch_warnings():
# Pydantic >= 2.12.0 warns about field specific metadata that is unused
# (e.g. `TypeAdapter(Annotated[int, Field(alias='b')])`). In some cases, we
# end up building the type adapter from a model field annotation so we
# need to ignore the warning:
if shared.PYDANTIC_VERSION_MINOR_TUPLE >= (2, 12):
from pydantic.warnings import UnsupportedFieldAttributeWarning
warnings.simplefilter(
"ignore", category=UnsupportedFieldAttributeWarning
)
# TODO: remove after setting the min Pydantic to v2.12.3
# that adds asdict(), and use self.field_info.asdict() instead
field_dict = asdict(self.field_info)
annotated_args = (
field_dict["annotation"],
*field_dict["metadata"],
# this FieldInfo needs to be created again so that it doesn't include
# the old field info metadata and only the rest of the attributes
Field(**field_dict["attributes"]),
)
self._type_adapter: TypeAdapter[Any] = TypeAdapter(
Annotated[annotated_args],
config=self.config,
)
def get_default(self) -> Any:
if self.field_info.is_required():
return Undefined
return self.field_info.get_default(call_default_factory=True)
def validate(
self,
value: Any,
values: dict[str, Any] = {}, # noqa: B006
*,
loc: tuple[Union[int, str], ...] = (),
) -> tuple[Any, list[dict[str, Any]]]:
try:
return (
self._type_adapter.validate_python(value, from_attributes=True),
[],
)
except ValidationError as exc:
return None, _regenerate_error_with_loc(
errors=exc.errors(include_url=False), loc_prefix=loc
)
def serialize(
self,
value: Any,
*,
mode: Literal["json", "python"] = "json",
Domain
Defined In
Source
Frequently Asked Questions
What is the ModelField class?
ModelField is a class in the fastapi codebase, defined in fastapi/_compat/v2.py.
Where is ModelField defined?
ModelField is defined in fastapi/_compat/v2.py at line 82.
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free