BaseModel
A base class for creating Pydantic models.
Attributes
| Attribute | Type | Description |
|---|---|---|
| model_config | ClassVar[[ConfigDict](../v1/config/configdict.md?sid=pydantic_v1_config_configdict)] = ConfigDict() | Configuration for the model, should be a dictionary conforming to ConfigDict. |
Constructor
Signature
def BaseModel(
data: Any
) - > None
Parameters
| Name | Type | Description |
|---|---|---|
| data | Any | Keyword arguments representing the model data to be validated. |
Signature
def BaseModel(
data: Any
) - > None
Parameters
| Name | Type | Description |
|---|---|---|
| data | Any | The keyword arguments representing model fields to be validated and assigned |
Methods
model_fields()
@classmethod
def model_fields() - > dict[str, [FieldInfo](../v1/fields/fieldinfo.md?sid=pydantic_v1_fields_fieldinfo)]
A mapping of field names to their respective FieldInfo instances.
Returns
| Type | Description |
|---|---|
dict[str, [FieldInfo](../v1/fields/fieldinfo.md?sid=pydantic_v1_fields_fieldinfo)] | A dictionary mapping field names to their metadata objects |
model_computed_fields()
@classmethod
def model_computed_fields() - > dict[str, [ComputedFieldInfo](../fields/computedfieldinfo.md?sid=pydantic_fields_computedfieldinfo)]
A mapping of computed field names to their respective ComputedFieldInfo instances.
Returns
| Type | Description |
|---|---|
dict[str, [ComputedFieldInfo](../fields/computedfieldinfo.md?sid=pydantic_fields_computedfieldinfo)] | A dictionary mapping computed field names to their metadata objects |
model_extra()
@classmethod
def model_extra() - > dict[str, Any]| None
Get extra fields set during validation.
Returns
| Type | Description |
|---|---|
| `dict[str, Any] | None` |
model_fields_set()
@classmethod
def model_fields_set() - > set[str]
Returns the set of fields that have been explicitly set on this model instance.
Returns
| Type | Description |
|---|---|
set[str] | A set of strings representing the fields that have been set, i.e. that were not filled from defaults |
model_construct()
@classmethod
def model_construct(
_fields_set: set[str]| None = None,
values: Any
) - > Self
Creates a new instance of the Model class with validated data.
Parameters
| Name | Type | Description |
|---|---|---|
| _fields_set | `set[str] | None` = None |
| values | Any | Trusted or pre-validated data dictionary |
Returns
| Type | Description |
|---|---|
Self | A new instance of the Model class with validated data |
model_copy()
@classmethod
def model_copy(
update: Mapping[str, Any]| None = None,
deep: bool = False
) - > Self
Returns a copy of the model.
Parameters
| Name | Type | Description |
|---|---|---|
| update | `Mapping[str, Any] | None` = None |
| deep | bool = False | Whether to perform a deep copy of the model |
Returns
| Type | Description |
|---|---|
Self | New model instance |
model_dump()
@classmethod
def model_dump(
mode: Literal['json', 'python']| str = 'python',
include: IncEx | None = None,
exclude: IncEx | None = None,
context: Any | None = None,
by_alias: bool | None = None,
exclude_unset: bool = False,
exclude_defaults: bool = False,
exclude_none: bool = False,
exclude_computed_fields: bool = False,
round_trip: bool = False,
warnings: bool | Literal['none', 'warn', 'error'] = True,
fallback: Callable[[Any], Any]| None = None,
serialize_as_any: bool = False,
polymorphic_serialization: bool | None = None
) - > dict[str, Any]
Generate a dictionary representation of the model, optionally specifying which fields to include or exclude.
Parameters
| Name | Type | Description |
|---|---|---|
| mode | `Literal['json', 'python'] | str` = 'python' |
| include | `IncEx | None` = None |
| exclude | `IncEx | None` = None |
| context | `Any | None` = None |
| by_alias | `bool | None` = None |
| exclude_unset | bool = False | Whether to exclude fields that have not been explicitly set |
| exclude_defaults | bool = False | Whether to exclude fields that are set to their default value |
| exclude_none | bool = False | Whether to exclude fields that have a value of None |
| exclude_computed_fields | bool = False | Whether to exclude computed fields from the output |
| round_trip | bool = False | Whether dumped values should be valid as input for non-idempotent types |
| warnings | `bool | Literal['none', 'warn', 'error']` = True |
| fallback | `Callable[[Any], Any] | None` = None |
| serialize_as_any | bool = False | Whether to serialize fields with duck-typing serialization behavior |
| polymorphic_serialization | `bool | None` = None |
Returns
| Type | Description |
|---|---|
dict[str, Any] | A dictionary representation of the model |
model_dump_json()
@classmethod
def model_dump_json(
indent: int | None = None,
ensure_ascii: bool = False
) - > str
Generates a JSON representation of the model using Pydantic's to_json method.
Parameters
| Name | Type | Description |
|---|---|---|
| indent | `int | None` = None |
| ensure_ascii | bool = False | Whether to escape all incoming non-ASCII characters |
Returns
| Type | Description |
|---|---|
str | A JSON string representation of the model |
model_json_schema()
@classmethod
def model_json_schema(
by_alias: bool = True,
ref_template: str = DEFAULT_REF_TEMPLATE,
schema_generator: type[[GenerateJsonSchema](../json/schema/generatejsonschema.md?sid=pydantic_json_schema_generatejsonschema)] = GenerateJsonSchema,
mode: JsonSchemaMode = 'validation',
union_format: Literal['any_of', 'primitive_type_array'] = 'any_of'
) - > dict[str, Any]
Generates a JSON schema for a model class.
Parameters
| Name | Type | Description |
|---|---|---|
| by_alias | bool = True | Whether to use attribute aliases in the schema |
| ref_template | str = DEFAULT_REF_TEMPLATE | The reference template for JSON pointers |
| schema_generator | type[[GenerateJsonSchema](../json/schema/generatejsonschema.md?sid=pydantic_json_schema_generatejsonschema)] = GenerateJsonSchema | The class used to generate the JSON schema |
| mode | JsonSchemaMode = 'validation' | The mode in which to generate the schema |
| union_format | Literal['any_of', 'primitive_type_array'] = 'any_of' | The format to use when combining schemas from unions |
Returns
| Type | Description |
|---|---|
dict[str, Any] | The JSON schema for the given model class |
model_parametrized_name()
@classmethod
def model_parametrized_name(
params: tuple[type[Any], ...]
) - > str
Compute the class name for parametrizations of generic classes.
Parameters
| Name | Type | Description |
|---|---|---|
| params | tuple[type[Any], ...] | Tuple of types used to parametrize the generic class |
Returns
| Type | Description |
|---|---|
str | String representing the new class where params are passed to cls as type variables |
model_post_init()
@classmethod
def model_post_init(
context: Any
) - > None
Override this method to perform additional initialization after init and model_construct.
Parameters
| Name | Type | Description |
|---|---|---|
| context | Any | The validation context passed during initialization |
Returns
| Type | Description |
|---|---|
None |
model_rebuild()
@classmethod
def model_rebuild(
force: bool = False,
raise_errors: bool = True
) - > bool | None
Try to rebuild the pydantic-core schema for the model.
Parameters
| Name | Type | Description |
|---|---|---|
| force | bool = False | Whether to force the rebuilding of the model schema |
| raise_errors | bool = True | Whether to raise errors if rebuilding fails |
Returns
| Type | Description |
|---|---|
| `bool | None` |
model_validate()
@classmethod
def model_validate(
obj: Any,
strict: bool | None = None,
from_attributes: bool | None = None
) - > Self
Validate a pydantic model instance.
Parameters
| Name | Type | Description |
|---|---|---|
| obj | Any | The object to validate against the model schema |
| strict | `bool | None` = None |
| from_attributes | `bool | None` = None |
Returns
| Type | Description |
|---|---|
Self | The validated model instance |
model_validate_json()
@classmethod
def model_validate_json(
json_data: str | bytes | bytearray
) - > Self
Validate the given JSON data against the Pydantic model.
Parameters
| Name | Type | Description |
|---|---|---|
| json_data | `str | bytes |
Returns
| Type | Description |
|---|---|
Self | The validated Pydantic model |
model_validate_strings()
@classmethod
def model_validate_strings(
obj: Any
) - > Self
Validate the given object with string data against the Pydantic model.
Parameters
| Name | Type | Description |
|---|---|---|
| obj | Any | The object containing string data to validate |
Returns
| Type | Description |
|---|---|
Self | The validated Pydantic model |