Handling Unset Values in Fastapi With Pydantic

Handling Unset Values in Fastapi With Pydantic 图片 1

In update API endpoints that allow for partial updates (aka PATCH updates), we need to know if a model field has been explicitly set by the caller.

Usually, we use None to indicate an unset value. But if None is also a valid value, how can we distinguish between None being explicitly set and None meaning “unset”?

For example, let’s say we have an optional field in the user profile, like a phone number. When using the user profile API endpoint, it’s not clear what to do if the phone number value is None. Should we reset it or keep it as it is?

添加评论
点赞收藏
点踩分享查看原文
评论
?
参与讨论