鉴于 :
class BGPcommunitiesElasticSchema(marshmallow.Schema):
comm_name = marshmallow.fields.Str(required=True)
comm_value = marshmallow.fields.Str(required=True, )
dev_name = marshmallow.fields.Str(required=True)
time_stamp = marshmallow.fields.Integer(missing=time.time())
@marshmallow.validates('comm_value')
def check_comm_value(self, value):
if value.count(":") < 1:
raise marshmallow.ValidationError("a BGP community value should contain at least once the colon char")
if value.count(":") > 2:
raise marshmallow.ValidationError("a BGP community value should contain no more than two colon chars")
# @marshmallow.pre_dump
# def rename_comm_value(self, data):
# return data['comm_value'].replace(":","_")
comm_value
在序列化之前如何操作该字段?
例如,该字段comm_value
是一个字符串1234:5678
,我想将其转换为1234_5678
.
您能否就如何实现这一目标提出建议?
PS。pre_dump
看起来是正确的做法,但我不确定,因为这是我第一天使用marshmallow