Is there a way to order a nested serializer _set
, for example order by pk
or time-stamp
.
So basically order song_set
You can use SerializerMethodField
and write custom method for this.
class AlbumSerializer(HyperlinkedModelSerializer):
song_set = serializers.SerializerMethodField()
class Meta:
model = Album
fields = [
'pk',
'timestamp',
'song_set'
]
def get_song_set(self, instance):
songs = instance.song_set.all().order_by('-timestamp')
return SongListSerializer(songs, many=True).data