Pydantic: Detect if a field value is missing or given as null

We Are Going To Discuss About Pydantic: Detect if a field value is missing or given as null. So lets Start this Python Article.

Pydantic: Detect if a field value is missing or given as null

  1. How to solve Pydantic: Detect if a field value is missing or given as null

    The pydantic documentation desccribes two options that can be used with the .dict() method of models.
    exclude_unset: whether fields which were not explicitly set when creating the model should be excluded from the returned dictionary; default False. Prior to v1.0, exclude_unset was known as skip_defaults; use of skip_defaults is now deprecated
    exclude_defaults: whether fields which are equal to their default values (whether set or otherwise) should be excluded from the returned dictionary; default False
    So you can create a model class with optional fields:
    from typing import Optional from pydantic import BaseModel class MyModel(BaseModel): foo: Optional[int] = None bar: Optional[int] = None
    And still generate a dict with fields explicitely set to None, but without default values:
    baz = MyModel(foo=None) assert baz.dict(exclude_unset=True) == {"foo": None} baz = MyModel(bar=None) assert baz.dict(exclude_unset=True) == {"bar": None}

  2. Pydantic: Detect if a field value is missing or given as null

    The pydantic documentation desccribes two options that can be used with the .dict() method of models.
    exclude_unset: whether fields which were not explicitly set when creating the model should be excluded from the returned dictionary; default False. Prior to v1.0, exclude_unset was known as skip_defaults; use of skip_defaults is now deprecated
    exclude_defaults: whether fields which are equal to their default values (whether set or otherwise) should be excluded from the returned dictionary; default False
    So you can create a model class with optional fields:
    from typing import Optional from pydantic import BaseModel class MyModel(BaseModel): foo: Optional[int] = None bar: Optional[int] = None
    And still generate a dict with fields explicitely set to None, but without default values:
    baz = MyModel(foo=None) assert baz.dict(exclude_unset=True) == {"foo": None} baz = MyModel(bar=None) assert baz.dict(exclude_unset=True) == {"bar": None}

Solution 1

The pydantic documentation desccribes two options that can be used with the .dict() method of models.

  • exclude_unset: whether fields which were not explicitly set when creating the model should be excluded from the returned dictionary; default False. Prior to v1.0, exclude_unset was known as skip_defaults; use of skip_defaults is now deprecated

  • exclude_defaults: whether fields which are equal to their default values (whether set or otherwise) should be excluded from the returned dictionary; default False

So you can create a model class with optional fields:

from typing import Optional
from pydantic import BaseModel


class MyModel(BaseModel):
    foo: Optional[int] = None
    bar: Optional[int] = None

And still generate a dict with fields explicitely set to None, but without default values:

baz = MyModel(foo=None)
assert baz.dict(exclude_unset=True) == {"foo": None}

baz = MyModel(bar=None)
assert baz.dict(exclude_unset=True) == {"bar": None}

Original Author gcharbon Of This Content

Solution 2

You can check obj.__fields_set__ to see whether the value was missing or not.

from typing import Optional
from pydantic import BaseModel

class Foo(BaseModel):
    first: Optional[int] = None
    second: Optional[int] = None

foo = Foo.parse_raw('{"first": null}')

assert foo.first is None and foo.second is None
assert foo.__fields_set__ == {"first"}

Original Author alex_noname Of This Content

Conclusion

So This is all About This Tutorial. Hope This Tutorial Helped You. Thank You.

Also Read,

ittutorial team

I am an Information Technology Engineer. I have Completed my MCA And I have 4 Year Plus Experience, I am a web developer with knowledge of multiple back-end platforms Like PHP, Node.js, Python and frontend JavaScript frameworks Like Angular, React, and Vue.

Leave a Comment