Skip to main content

BaseModel

A base class for creating Pydantic models.

Attributes

AttributeTypeDescription
model_configClassVar[[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

NameTypeDescription
dataAnyKeyword arguments representing the model data to be validated.

Signature

def BaseModel(
data: Any
) - > None

Parameters

NameTypeDescription
dataAnyThe 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

TypeDescription
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

TypeDescription
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

TypeDescription
`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

TypeDescription
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

NameTypeDescription
_fields_set`set[str]None` = None
valuesAnyTrusted or pre-validated data dictionary

Returns

TypeDescription
SelfA 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

NameTypeDescription
update`Mapping[str, Any]None` = None
deepbool = FalseWhether to perform a deep copy of the model

Returns

TypeDescription
SelfNew 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

NameTypeDescription
mode`Literal['json', 'python']str` = 'python'
include`IncExNone` = None
exclude`IncExNone` = None
context`AnyNone` = None
by_alias`boolNone` = None
exclude_unsetbool = FalseWhether to exclude fields that have not been explicitly set
exclude_defaultsbool = FalseWhether to exclude fields that are set to their default value
exclude_nonebool = FalseWhether to exclude fields that have a value of None
exclude_computed_fieldsbool = FalseWhether to exclude computed fields from the output
round_tripbool = FalseWhether dumped values should be valid as input for non-idempotent types
warnings`boolLiteral['none', 'warn', 'error']` = True
fallback`Callable[[Any], Any]None` = None
serialize_as_anybool = FalseWhether to serialize fields with duck-typing serialization behavior
polymorphic_serialization`boolNone` = None

Returns

TypeDescription
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

NameTypeDescription
indent`intNone` = None
ensure_asciibool = FalseWhether to escape all incoming non-ASCII characters

Returns

TypeDescription
strA 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

NameTypeDescription
by_aliasbool = TrueWhether to use attribute aliases in the schema
ref_templatestr = DEFAULT_REF_TEMPLATEThe reference template for JSON pointers
schema_generatortype[[GenerateJsonSchema](../json/schema/generatejsonschema.md?sid=pydantic_json_schema_generatejsonschema)] = GenerateJsonSchemaThe class used to generate the JSON schema
modeJsonSchemaMode = 'validation'The mode in which to generate the schema
union_formatLiteral['any_of', 'primitive_type_array'] = 'any_of'The format to use when combining schemas from unions

Returns

TypeDescription
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

NameTypeDescription
paramstuple[type[Any], ...]Tuple of types used to parametrize the generic class

Returns

TypeDescription
strString 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

NameTypeDescription
contextAnyThe validation context passed during initialization

Returns

TypeDescription
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

NameTypeDescription
forcebool = FalseWhether to force the rebuilding of the model schema
raise_errorsbool = TrueWhether to raise errors if rebuilding fails

Returns

TypeDescription
`boolNone`

model_validate()

@classmethod
def model_validate(
obj: Any,
strict: bool | None = None,
from_attributes: bool | None = None
) - > Self

Validate a pydantic model instance.

Parameters

NameTypeDescription
objAnyThe object to validate against the model schema
strict`boolNone` = None
from_attributes`boolNone` = None

Returns

TypeDescription
SelfThe 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

NameTypeDescription
json_data`strbytes

Returns

TypeDescription
SelfThe 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

NameTypeDescription
objAnyThe object containing string data to validate

Returns

TypeDescription
SelfThe validated Pydantic model