Skip to content

Commit

Permalink
refactor: change configuration for mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
johanseto committed Jul 10, 2023
1 parent 9bd8f11 commit e371330
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions eox_nelp/course_experience/api/v1/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
}

Expand Down Expand Up @@ -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,
Expand All @@ -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("__"):
Expand Down

0 comments on commit e371330

Please sign in to comment.