From b16ec214868a7bd1388bb7ca03880aea5c5a51d5 Mon Sep 17 00:00:00 2001 From: Alexander-D-Karpov Date: Sun, 17 Dec 2023 16:36:30 +0300 Subject: [PATCH] fixed music artist and albums relations --- akarpov/music/api/serializers.py | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/akarpov/music/api/serializers.py b/akarpov/music/api/serializers.py index c74df67..95a13f3 100644 --- a/akarpov/music/api/serializers.py +++ b/akarpov/music/api/serializers.py @@ -243,10 +243,16 @@ class FullAlbumSerializer(serializers.ModelSerializer): @extend_schema_field(AuthorSerializer(many=True)) def get_artists(self, obj): + artists = [] + qs = Author.objects.cache().filter( + songs__id__in=obj.songs.cache().all().values("id").distinct() + ) + for artist in qs: + if artist not in artists: + artists.append(artist) + return AuthorSerializer( - Author.objects.cache().filter( - songs__id__in=obj.songs.cache().all().values("id") - ), + artists, many=True, ).data @@ -265,10 +271,17 @@ class FullAuthorSerializer(serializers.ModelSerializer): @extend_schema_field(AlbumSerializer(many=True)) def get_albums(self, obj): + qs = Album.objects.cache().filter( + songs__id__in=obj.songs.cache().all().values("id").distinct() + ) + albums = [] + for album in qs: + # TODO: rewrite to filter + if album not in albums: + albums.append(album) + return AlbumSerializer( - Album.objects.cache().filter( - songs__id__in=obj.songs.cache().all().values("id") - ), + albums, many=True, ).data