[Answered ]-How to remove nesting from Django REST Framework serializer?

1👍

SongSerializer

  1. remove album = AlbumSerializer()

  2. override method to_representation

from django.forms import model_to_dict

def to_representation(self, instance):
        song = super().to_representation(instance)
        album = model_to_dict(instance.album)
        for key, value in album.items():
           setattr(song, key, value)
        return song

I did not test the code.

0👍

Try this:

class SongSerializer(serializers.ModelSerializer):
    album_id = serializers.SerializerMethodField(source='album.id')
    album = serializers.SerializerMethodField(source='album.album')
    band = serializers.SerializerMethodField(source='album.band')

    class Meta:
        model = Song
        fields = ['album_id', 'album', 'band', .....]

Leave a comment