From e371330e4d35fd33a0f1220ae6d6650dba3356c8 Mon Sep 17 00:00:00 2001 From: Johan Castiblanco Date: Mon, 10 Jul 2023 14:15:06 -0500 Subject: [PATCH] refactor: change configuration for mapping --- eox_nelp/course_experience/api/v1/serializers.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/eox_nelp/course_experience/api/v1/serializers.py b/eox_nelp/course_experience/api/v1/serializers.py index 6d635b2e..ad96b7cd 100644 --- a/eox_nelp/course_experience/api/v1/serializers.py +++ b/eox_nelp/course_experience/api/v1/serializers.py @@ -19,7 +19,7 @@ USER_EXTRA_FIELD_MAPPING = { "first_name": "first_name", "last_name": "last_name", - "profile__name": "profile_name", + "profile_name": "profile__name", "username": "username", } @@ -58,17 +58,17 @@ def map_attributes_from_instance_to_dict(instance, attributes_mapping): attribute to look in the instance and the value the key name in the output dict. Based in the `attributes_mapping` you should use a dict with the following config: { - "field_name": "key_name" + "key_name": "field_name" } This would check in the instace instance.field_name and the value send it to output dict like {"key_name": instance.field_name} Also its is possible to check nested fields if you declarate the field of instance separated by `__` eg: { - "field_level1__field_level2": "key_name" + "key_name": "field_level1__field_level2" } This example would check in the instace like instance.field_level1.field_level2 and in the output - dict like {"key_name": nstance.field_level1.field_level2} + dict like {"key_name": instance.field_level1.field_level2} Args: instance (instance class): Model or instance of class to retrieved fields. attributes_mapping (dict): Dictionary map that has the fields to search and the keys name to output, @@ -77,7 +77,7 @@ def map_attributes_from_instance_to_dict(instance, attributes_mapping): instance_dict: dict representing the instance """ instance_dict = {} - for instance_field, extra_field in attributes_mapping.items(): + for extra_field, instance_field in attributes_mapping.items(): extra_value = None instance_level = copy(instance) for instance_field in instance_field.split("__"):