diff --git a/changes/1681-targhs.md b/changes/1681-targhs.md new file mode 100644 index 0000000000..71734cc7ce --- /dev/null +++ b/changes/1681-targhs.md @@ -0,0 +1 @@ +Update documentation for schema field customization alias diff --git a/docs/examples/schema_field_customization_alias.py b/docs/examples/schema_field_customization_alias.py new file mode 100644 index 0000000000..8935c8b486 --- /dev/null +++ b/docs/examples/schema_field_customization_alias.py @@ -0,0 +1,9 @@ +from pydantic import BaseModel, Field + + +class Foo(BaseModel): + private_name: int = Field(0, alias='public_name') + + +foo = Foo(public_name=1) +foo.private_name # returns 1 diff --git a/docs/usage/schema.md b/docs/usage/schema.md index 458014a415..45da5c4b17 100644 --- a/docs/usage/schema.md +++ b/docs/usage/schema.md @@ -61,7 +61,12 @@ It has the following arguments: * `default_factory`: a zero-argument callable that will be called when a default value is needed for this field. Among other purposes, this can be used to set dynamic default values. It is forbidden to set both `default` and `default_factory`. -* `alias`: the public name of the field +* `alias`: the public name of the field. The name maps to a field during initialization. + +```py +{!.tmp_examples/schema_field_customization_alias.py!} +``` + * `title`: if omitted, `field_name.title()` is used * `description`: if omitted and the annotation is a sub-model, the docstring of the sub-model will be used