[Django]-Django rest framework – serializing related object fields to a flat json dict

0👍

Well I don’t guess there is some in-built Python functionality to flatten a dictionary, but you can write one by using the concept of recursion. The idea is to iterate over all the key, value pairs and see if the value is a dictionary, then call the recursive method, else just update the flatten dictionary with the key, value

def flatten_dict(dictionary, flatten_dictionary):
    for k, v in dictionary.iteritems():
        if not isinstance(v, dict):
            flatten_dictionary[k] = v
        else:
            flatten_dict(v, flatten_dictionary)

f_dict = {}
flatten_dict(d, f_dict)
print f_dict
>>> {'field2': 'val2', 'related_obj_field_1': 'val1', 'related_obj_field_2': 'val2', 'field1': 'val1'}
👤ZdaR

0👍

Clear way for achieving it is using to_representation() method of Serializer. It helps modify serializer’s data before converting to json:

class OrganizationUsersSerializer(serializers.ModelSerializer):
    user = UserSerializer()

    class Meta:
        model = UserOrganizations
        fields = ['organization_name', 'user']

    def to_representation(self, instance):
        data = super(OrganizationUsersSerializer, self).to_representation(instance)
        profile = data.pop('user')
        for key, val in profile.items():
            data.update({key: val})
        return data

So, you remove nested object and add its fields(specified in its own Serializer) to the same level.

Leave a comment