1👍
✅
SongSerializer
-
remove
album = AlbumSerializer()
-
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', .....]
- [Answered ]-MySQL converting enum values to integer values
- [Answered ]-AttributeError at / type object 'Form' has no attribute 'objects', how to fix?
- [Answered ]-How to get data from config file (config.json) in real time in settings.py file in django
- [Answered ]-Getting started with django and heroku, "application error" after upload
- [Answered ]-Passing a tuple in a Google charts table using Django
Source:stackexchange.com