Django DRF how to use to_representation and to_internal_value? -
the documentation brief, , i'm unable figure out how use reading source code.
here want accomplish in pseudocode:
def transform_result(self, data): ret = {} entry in data: type = entry['type'] if type in ret: ret[type].add({entry['id'], entry['tag']}) else: new_type = ret.add(type) new_type.add({entry['id'], entry['tag']}) return ret
which should take this:
[ { "id": 1, "type": "color", "tag": "blue" }, { "id": 2, "type": "color", "tag": "red" }, { "id": 3, "type": "shape", "tag": "square" }, ]
and transform this:
[ { "type": "color", "tags": [ { "id": 1, "tag": "blue" }, { "id": 2, "tag": "red" } ] }, { "type": "shape", "tags": [ { "id": 3, "tag": "square" } ] } ]
should done using to_representation
, to_internal_value
? if so, how done?
Comments
Post a Comment