ModelField
This class is a TypedDict that defines the configuration and metadata for a specific field within a model. It specifies essential properties such as the core schema and field type, while providing optional configurations for validation aliases, serialization behavior, and immutability.
Attributes
| Attribute | Type | Description |
|---|---|---|
| type | Literal['model-field'] | A fixed literal string 'model-field' used to identify the schema type within the Pydantic core schema structure. |
| schema | CoreSchema | The underlying Pydantic core schema that defines the validation and serialization logic for the field's value. |
| validation_alias | `str | list[str |
| serialization_alias | str | The key name used for this field when exporting the model to a dictionary or JSON string. |
| serialization_exclude | bool = False | Determines whether this field should be omitted entirely from the output during serialization. |
| serialization_exclude_if | Callable[[Any], bool] = None | A callable that takes the field value and returns a boolean to determine if the field should be excluded from serialization dynamically. |
| frozen | bool | Indicates if the field is immutable, preventing its value from being changed after the model instance is initialized. |
| metadata | dict[str, Any] | A dictionary of arbitrary data associated with the field, often used for custom validation logic or third-party integrations. |
Methods
type()
def type() - > string
Specifies the fixed identifier for this schema component, used to distinguish it as a model field within the Pydantic core schema.
Returns
| Type | Description |
|---|---|
string | The literal string 'model-field' identifying the schema type. |
schema()
def schema() - > CoreSchema
Defines the inner Pydantic core schema used to validate and serialize the data within this specific field.
Returns
| Type | Description |
|---|---|
CoreSchema | The core schema definition for the field's value. |
validation_alias()
def validation_alias() - > string | list[str | int]| list[list[str | int]]
Configures an alternative name or path used to locate the field value during the validation process.
Returns
| Type | Description |
|---|---|
| `string | list[str |
serialization_alias()
def serialization_alias() - > string
Sets the name used for the field when the model is serialized into a dictionary or JSON string.
Returns
| Type | Description |
|---|---|
string | The output key name for the field. |
serialization_exclude()
def serialization_exclude() - > boolean
Determines whether this field should be omitted entirely from the serialized output of the model.
Returns
| Type | Description |
|---|---|
boolean | True if the field should be excluded, False otherwise. |
serialization_exclude_if()
def serialization_exclude_if() - > Callable[[Any], bool]
Provides a predicate function to conditionally exclude the field from serialization based on its current value.
Returns
| Type | Description |
|---|---|
Callable[[Any], bool] | A function that returns True if the field should be excluded for the given value. |
frozen()
def frozen() - > boolean
Indicates whether the field is immutable; if True, the field cannot be assigned a new value after the model is initialized.
Returns
| Type | Description |
|---|---|
boolean | True if the field is read-only, False otherwise. |
metadata()
def metadata() - > dict[str, Any]
Stores a dictionary of arbitrary metadata associated with the field, often used by third-party tools or custom validation logic.
Returns
| Type | Description |
|---|---|
dict[str, Any] | A dictionary containing custom metadata key-value pairs. |