Given that :
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(":","_")
How can I manipulate the field comm_value
before serializing it ?
The field comm_value
is a string for example 1234:5678
and I would like to convert it to 1234_5678
.
Could you please advise on how to achieve that ?
PS. pre_dump
looks the right way of doing it, but I am not sure since it is my first day using marshmallow