python - 如何在 Python 中从 field.Method 访问不同的参数
问题描述
我想将我的邮政编码更改为 ''
如果它None
但无法正确访问 country_code 参数来执行此操作。我究竟做错了什么?
class AddressSchema(Schema):
def _postal_check(self, postal):
allowed_countries = ["GE","BT","HK","MO"]
postal_code = postal.postalCode
country_code = postal.countryCode
if postal_code is None and country_code in allowed_countries:
postal_code = ''
return postal_code
countryCode = fields.Str(validate=Length(equal=2), required=True)
postalCode = fields.Method(serialize='_postal_check', allow_none=True, required=True)
解决方案
问题是我试图将邮政作为一个对象而不是作为字典来访问,所以解决方案是
class AddressSchema(Schema):
def _postal_check(self, postal):
allowed_countries = ["GE","BT","HK","MO"]
postal_code = postal['postalCode']
country_code = postal['countryCode']
if postal_code is None and country_code in allowed_countries:
postal_code = ''
return postal_code
country_code = fields.Str(validate=Length(equal=2), required=True)
postal_code = fields.Method(serialize='_postal_check', allow_none=True, required=True)
推荐阅读
- swift - Swift 4.2 轮函数
- vba - Excel VBA 保存到主工作表
- jenkins - 将远程存储库从 Jenkins 拉到 ubuntu 目录时出错
- r - data.table::set 与其他 data.frame 交互
- java - 发布 .apk 不起作用,相机活动崩溃
- rust - 在遍历同一 Vec 中另一个元素的字段时改变 Vec 元素的字段
- git - Unity & git:多项目结构和符号链接,更好的解决方案?
- c# - 引用表“x”中没有与引用列外键“.y”匹配的主表
- ios - 协议要求委托继承自 UIViewController
- linux - File(1) 将空字节分类为数据文件