Pydantic: Is there a way to have a field be a representation of two fields

Created on 14 Sep 2020  路  3Comments  路  Source: samuelcolvin/pydantic

Output of: python -c "import pydantic.utils; print(pydantic.utils.version_info())"

pydantic version: 1.6.1
pydantic compiled: True
install path: /Users/rrogers/.pyenv/versions/3.7.8/lib/python3.7/site-packages/pydantic
python version: 3.7.8 (default, Aug 24 2020, 11:23:48)  [Clang 11.0.3 (clang-1103.0.32.62)]
platform: Darwin-19.6.0-x86_64-i386-64bit
optional deps. installed: ['typing-extensions']

What I am trying to do here is have two fields make up a third field like so:

class Person(BaseModel):
    first_name: str
    last_name: str
    full_name: str = first_name + last_name

From what I can tell this isn't possible? but hoping someone has come up with a solution, that allows it to be done during object instantiation.

question

Most helpful comment

You can do it pretty easily with a validator, though you will need to use the always flag to ensure it runs when the value isn't provided:

In [1]: from pydantic import BaseModel, validator
   ...:
   ...: class Person(BaseModel):
   ...:     first_name: str
   ...:     last_name: str
   ...:     full_name: str = None
   ...:
   ...:     @validator("full_name", always=True)
   ...:     def composite_name(cls, v, values, **kwargs):
   ...:         return " ".join([values['first_name'], values['last_name']])
   ...:
In [2]: Person(first_name="Robert", last_name="Rogers")
Out[2]: Person(first_name='Robert', last_name='Rogers', full_name='Robert Rogers')

See: https://pydantic-docs.helpmanual.io/usage/validators/#validate-always

Also, please follow the Issue templates. The checkboxes aren't meant to be ignored.

All 3 comments

You can do it pretty easily with a validator, though you will need to use the always flag to ensure it runs when the value isn't provided:

In [1]: from pydantic import BaseModel, validator
   ...:
   ...: class Person(BaseModel):
   ...:     first_name: str
   ...:     last_name: str
   ...:     full_name: str = None
   ...:
   ...:     @validator("full_name", always=True)
   ...:     def composite_name(cls, v, values, **kwargs):
   ...:         return " ".join([values['first_name'], values['last_name']])
   ...:
In [2]: Person(first_name="Robert", last_name="Rogers")
Out[2]: Person(first_name='Robert', last_name='Rogers', full_name='Robert Rogers')

See: https://pydantic-docs.helpmanual.io/usage/validators/#validate-always

Also, please follow the Issue templates. The checkboxes aren't meant to be ignored.

Hello @bb-rrogers
Some day we may have a @computed_field decorator (see https://github.com/samuelcolvin/pydantic/issues/935#issuecomment-555036039).
In the meantime @StephenBrown2's solution is the way to go!

Thank you both. Apologies if I had formulated my question incorrectly or missed checkboxes. I appreciate the quick response given.

I will close this, and will track the comment regarding the computed_field validator.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

cazgp picture cazgp  路  34Comments

kryft picture kryft  路  35Comments

rrbarbosa picture rrbarbosa  路  35Comments

bradodarb picture bradodarb  路  22Comments

samuelcolvin picture samuelcolvin  路  30Comments