Model config
Behaviour of pydantic can be controlled via the Config
class on a model.
Options:
title
- title for the generated JSON Schema
anystr_strip_whitespace
- strip or not trailing and leading whitespace for str & byte types (default:
False
) min_anystr_length
- min length for str & byte types (default:
0
) max_anystr_length
- max length for str & byte types (default:
2 ** 16
) validate_all
- whether or not to validate field defaults (default:
False
) extra
- whether to ignore, allow or forbid extra attributes in model. Can use either string values of
ignore
,allow
orforbid
, or useExtra
enum (default isExtra.ignore
) allow_mutation
- whether or not models are faux-immutable, e.g. setattr fails (default:
True
) use_enum_values
- whether to populate models with the
value
property of enums, rather than the raw enum - useful if you want to serialisemodel.dict()
later (default:False
) fields
- schema information on each field, this is equivilant to
using the schema class (default:
None
) validate_assignment
- whether to perform validation on assignment to attributes or not (default:
False
) allow_population_by_field_name
- whether or not an aliased field may be populated by its name as given by the model
attribute, as well as the alias (default:
False
)
Note
The name of this configuration setting was changed in v1.0 from
allow_population_by_alias
to allow_population_by_field_name
.
error_msg_templates
- let's you to override default error message templates.
Pass in a dictionary with keys matching the error messages you want to override (default:
{}
) arbitrary_types_allowed
- whether to allow arbitrary user types for fields (they are validated simply by checking if the
value is instance of that type). If
False
-RuntimeError
will be raised on model declaration (default:False
) orm_mode
- allows usage of ORM mode
getter_dict
- custom class (should inherit from
GetterDict
) to use when decomposing ORM classes for validation, use withorm_mode
alias_generator
- callable that takes field name and returns alias for it
keep_untouched
- tuple of types (e. g. descriptors) that won't change during model creation and won't be included in the model schemas
schema_extra
- takes a
dict
to extend/update the generated JSON Schema json_loads
- custom function for decoding JSON, see custom JSON (de)serialisation
json_dumps
- custom function for encoding JSON, see custom JSON (de)serialisation
json_encoders
- customise the way types are encoded to JSON, see JSON Serialisation
from pydantic import BaseModel, ValidationError class Model(BaseModel): v: str class Config: max_anystr_length = 10 error_msg_templates = { 'value_error.any_str.max_length': 'max_length:{limit_value}', } try: Model(v='x' * 20) except ValidationError as e: print(e) """ 1 validation error v max_length:10 (type=value_error.any_str.max_length; limit_value=10) """
(This script is complete, it should run "as is")
Version for models based on @dataclass
decorator:
from datetime import datetime from pydantic import ValidationError from pydantic.dataclasses import dataclass class MyConfig: max_anystr_length = 10 validate_assignment = True error_msg_templates = { 'value_error.any_str.max_length': 'max_length:{limit_value}', } @dataclass(config=MyConfig) class User: id: int name: str = 'John Doe' signup_ts: datetime = None user = User(id='42', signup_ts='2032-06-21T12:00') try: user.name = 'x' * 20 except ValidationError as e: print(e) """ 1 validation error name max_length:10 (type=value_error.any_str.max_length; limit_value=10) """
(This script is complete, it should run "as is")
Alias Generator🔗
If data source field names do not match your code style (e. g. CamelCase fields),
you can automatically generate aliases using alias_generator
:
from pydantic import BaseModel def to_camel(string: str) -> str: return ''.join(word.capitalize() for word in string.split('_')) class Voice(BaseModel): name: str gender: str language_code: str class Config: alias_generator = to_camel voice = Voice(Name='Filiz', Gender='Female', LanguageCode='tr-TR') print(voice.language_code) print(voice.dict(by_alias=True)) """ tr-TR {'Name': 'Filiz', 'Gender': 'Female', 'LanguageCode': 'tr-TR'} """
(This script is complete, it should run "as is")