I have done a modification to modify key based on following requirements:
Excluding some keys Using key extendedValue if data is over of 254 chars.
I have written following code on serialiser:
@model_serializer(mode="wrap")
def _serialize(self, handler):
d = handler(self)
d_attributes = list()
for k, v in self.__dict__.items():
if k not in excluded:
data = { "name": k.upper() }
if len(v) < limit:
data.update({ "value": v })
else:
data.update({ "extendedValue": v })
d_attributes.append(data)
d["attributes"] = d_attributes
return d
But I receive following error:
Error serializing to JSON: PydanticSerializationError: Error calling function `_serialize`: ValueError: Circular reference detected (id repeated)
Instead if I rollback with following code:
@model_serializer(mode="wrap")
def _serialize(self, handler):
d = handler(self)
d_attributes = list()
for k, v in self.__dict__.items():
if k not in excluded:
data = { "name": k.upper(), "value": v }
d_attributes.append(data)
d["attributes"] = d_attributes
return d
It works. Any Idea?