pydantic学习与使用-14.exclude_unset去掉未传值的字段

前言

使用 pydantic 定义数据模型,有些非必填的字段,我们希望在实例化时未传值的字段去掉,这样可以取值一些为None的字段。

遇到问题

age 和 address 是非必填字段

class User(BaseModel):
    name: str = ...
    tel: str = ...
    age: int = Field(None,)
    address: str = Field(None)


user = User(
    name='yoyo',
    tel='10086'
)
print(user.dict()) # {'name': 'yoyo', 'tel': '10086', 'age': None, 'address': None}

得到的结果, 没有传的字段会给到默认值

{'name': 'yoyo', 'tel': '10086', 'age': None, 'address': None}

exclude_unset去掉默认字段

可以通过 skip_defaults=True 参数跳过默认的设置项

print(user.dict(skip_defaults=True)) 

得到结果会有个警告:“skip_defaults” is deprecated and replaced by “exclude_unset”

DeprecationWarning: User.dict(): "skip_defaults" is deprecated and replaced by "exclude_unset"
  print(user.dict(skip_defaults=True)) # {'name': 'yoyo', 'tel': '10086', 'age': None, 'address': None}

{'name': 'yoyo', 'tel': '10086'}

“skip_defaults” 方法已经被 “exclude_unset” 替代了

print(user.dict(exclude_unset=True)) 

运行结果

{'name': 'yoyo', 'tel': '10086'}

这样就去掉了默认值为None的了。

报错了[root@iZuf6amkbc7gitbvijsvqyZ ~]# sudo yum update && sudo yum install xkeyboard-config Last metadata expiration check: 0:52:13 ago on Thu 20 Mar 2025 03:29:49 PM CST. Error: Problem 1: cannot install both gpgme-1.13.1-6.p01.ky10.x86_64 and gpgme-1.14.0-1.ky10.x86_64 - cannot install the best update candidate for package python3-gpgme-1.14.0-1.ky10.x86_64 - problem with installed package gpgme-1.14.0-1.ky10.x86_64 Problem 2: cannot install both kernel-devel-4.19.90-25.2.v2101.gfb01.ky10.x86_64 and kernel-devel-4.19.90-17.5.ky10.x86_64 - package kernel-devel-4.19.90-25.2.v2101.gfb01.ky10.x86_64 obsoletes kernel-devel < 4.19.90-25.2.v2101.gfb01.ky10 provided by kernel-devel-4.19.90-17.5.ky10.x86_64 - cannot install the best update candidate for package kernel-headers-4.19.90-24.4.v2101.ky10.x86_64 - cannot install the best update candidate for package kernel-devel-4.19.90-24.4.v2101.ky10.x86_64 Problem 3: cannot install both kernel-tools-4.19.90-25.2.v2101.gfb01.ky10.x86_64 and kernel-tools-4.19.90-17.5.ky10.x86_64 - cannot install the best update candidate for package kernel-tools-libs-4.19.90-24.4.v2101.ky10.x86_64 - cannot install the best update candidate for package kernel-tools-4.19.90-24.4.v2101.ky10.x86_64 Problem 4: package fcitx-qt5-1.2.4-3.1.ky10.x86_64 requires qt5-qtbase(x86-64) = 5.11.1, but none of the providers can be installed - cannot install both qt5-qtbase-5.12.5-6.gfb01.ky10.x86_64 and qt5-qtbase-5.11.1-9.ky10.x86_64 - cannot install the best update candidate for package qt5-qtbase-5.11.1-9.ky10.x86_64 - cannot install the best update candidate for package fcitx-qt5-1.2.4-3.1.ky10.x86_64 Problem 5: package libsoup-devel-2.70.0-1.ky10.x86_64 requires pkgconfig(libbrotlidec), but none of the providers can be installed - package brotli-devel-1.0.5-3.ky10.x86_64 requires brotli(x86-64) = 1.0.5-3.ky10, but none of the providers can be installed - package brotli-devel-1.0.7-1.ky10.x86_64 requires brotli(x86-64) = 1.0.7-1.k
03-21
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值