Files
pydantic/docs/usage/model_config.md
T
retnikt dccc4014dc Clarify documentation and error message about keep_untouched (#926)
* Clarify keep_untouched documentation (#924)

* Clarify error message for custom types (#924)

* Fix tests for changed error message (#924)

* fix formatting

* remove erroneous error message and add change
2019-10-23 11:14:51 +01:00

4.0 KiB

Behaviour of pydantic can be controlled via the Config class on a model.

Options:

title
the title for the generated JSON Schema
anystr_strip_whitespace
whether to strip leading and trailing whitespace for str & byte types (default: False)
min_anystr_length
the min length for str & byte types (default: 0)
max_anystr_length
the max length for str & byte types (default: 2 ** 16)
validate_all
whether to validate field defaults (default: False)
extra
whether to ignore, allow, or forbid extra attributes during model initialization. Accepts the string values of 'ignore', 'allow', or 'forbid', or values of the Extra enum (default: Extra.ignore)
allow_mutation
whether or not models are faux-immutable, i.e. whether __setattr__ is allowed (default: True)
use_enum_values
whether to populate models with the value property of enums, rather than the raw enum. This may be useful if you want to serialise model.dict() later (default: False)
fields
a dict containing schema information for each field; this is equivalent to using the schema class (default: None)
validate_assignment
whether to perform validation on assignment to attributes (default: False)
allow_population_by_field_name
whether 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
a dict used to override the 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 an instance of the type). If False, RuntimeError will be raised on model declaration (default: False)
orm_mode
whether to allow usage of ORM mode
getter_dict
a custom class (which should inherit from GetterDict) to use when decomposing ORM classes for validation, for use with orm_mode
alias_generator
a callable that takes a field name and returns an alias for it
keep_untouched
a tuple of types (e.g. descriptors) for a model's default values that should not be changed during model creation and will not be included in the model schemas. Note: this means that attributes on the model with defaults of this type, not annotations of this type, will be left alone.
schema_extra
a dict used to extend/update the generated JSON Schema
json_loads
a custom function for decoding JSON; see custom JSON (de)serialisation
json_dumps
a custom function for encoding JSON; see custom JSON (de)serialisation
json_encoders
a dict used to customise the way types are encoded to JSON; see JSON Serialisation
{!.tmp_examples/config.py!}

(This script is complete, it should run "as is")

Similarly, if using the @dataclass decorator:

{!.tmp_examples/ex_dataclasses_config.py!}

(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:

{!.tmp_examples/alias_generator_config.py!}

(This script is complete, it should run "as is")

Alias Precedence

Aliases defined on the Config class of child models will take priority over any aliases defined on Config of a parent model:

{!.tmp_examples/alias_precedence.py!}

(This script is complete, it should run "as is")

This includes when a child model uses alias_generator where the aliases of all parent model fields will be updated.