Django-restframework serialize relationships as dictionaries, not arrays

I am trying to serialize foreign keys as dictionaries instead of arrays. The json now looks like this:

{
"slug": "en",
"children": [{
        "slug": "pants",
        "children": [{
                "slug": "products/:level1",
                "children": [{
                    "slug": ":level2/:level3",
                    "children": []
                }]
            },
            {
                "slug": ":productSlug",
                "children": []
            }
        ]
    },
    {
        "slug": "pullovers",
        "children": []
    }
   ]
}

      

But I would like to use slugs as keys:

{
"en": {
    "children": {
        "pants": {
            "children": {
                "products/:level1": {
                    "children": {
                        ":level2/:level3": {
                            "children": {}
                        }
                    }
                }
            },
            ":productSlug": {
                "children": {}
            }
        ]
    }
 }
}

      

Is it possible to do this directly in the serializer or do I need to convert it for an extra step?

+3


source to share


2 answers


Perhaps by overriding the list serializers and setting alist_serializer_class

serializer for each one that requires it, as I suggested in this answer .

Of course you need to adjust it a bit:



class <YourClass>ListSerializer(serializers.ListSerializer):
    def to_representation(self, data):
        r = super().to_representation(data)

        return { item['<key_field>']: item for item in r }

      

+1


source


@ Michael Rigonis's answer ( fooobar.com/questions/2422153 / ... ) was the key to success. I had to tweak it a bit, so I was able to use it for the top level too:



class DictSerializer(serializers.ListSerializer):
    key = None

    def __init__(self, *args, **kwargs):
        self.key = kwargs.pop('key', self.key)
        super().__init__(*args, **kwargs)

    def to_representation(self, data):    
        r = super().to_representation(data)
        return {item[self.key]: item for item in r}

    @property
    def data(self):
        # This is a bit nasty, because the only "Many-Serializer" is a ListSerializer we inherit of it,
        # but when converting it to json we call the BaseSerializer directly, because we want a Dictionary rather then a list
        ret = super(serializers.ListSerializer, self).data
        return ReturnDict(ret, serializer=self)

      

+1


source







All Articles