fixed albums and authors, added related objects

This commit is contained in:
Alexander Karpov 2023-12-17 16:19:00 +03:00
parent 127b4b6e11
commit 7b48929e38
2 changed files with 26 additions and 8 deletions

View File

@ -239,10 +239,20 @@ class Meta:
class FullAlbumSerializer(serializers.ModelSerializer): class FullAlbumSerializer(serializers.ModelSerializer):
songs = ListSongSerializer(many=True, read_only=True) songs = ListSongSerializer(many=True, read_only=True)
artists = serializers.SerializerMethodField("get_artists")
@extend_schema_field(AuthorSerializer(many=True))
def get_artists(self, obj):
return AuthorSerializer(
Author.objects.cache().filter(
songs__id__in=obj.songs.cache().all().values("id")
),
many=True,
).data
class Meta: class Meta:
model = Album model = Album
fields = ["name", "link", "image", "songs"] fields = ["name", "link", "image", "songs", "artists"]
extra_kwargs = { extra_kwargs = {
"link": {"read_only": True}, "link": {"read_only": True},
"image": {"read_only": True}, "image": {"read_only": True},
@ -251,10 +261,20 @@ class Meta:
class FullAuthorSerializer(serializers.ModelSerializer): class FullAuthorSerializer(serializers.ModelSerializer):
songs = ListSongSerializer(many=True, read_only=True) songs = ListSongSerializer(many=True, read_only=True)
albums = serializers.SerializerMethodField(method_name="get_albums")
@extend_schema_field(AlbumSerializer(many=True))
def get_albums(self, obj):
return AlbumSerializer(
Album.objects.cache().filter(
songs__id__in=obj.songs.cache().all().values("id")
),
many=True,
).data
class Meta: class Meta:
model = Author model = Author
fields = ["name", "link", "image", "songs"] fields = ["name", "link", "image", "songs", "albums"]
extra_kwargs = { extra_kwargs = {
"link": {"read_only": True}, "link": {"read_only": True},
"image": {"read_only": True}, "image": {"read_only": True},

View File

@ -272,9 +272,7 @@ class ListAlbumsAPIView(generics.ListAPIView):
serializer_class = AlbumSerializer serializer_class = AlbumSerializer
pagination_class = StandardResultsSetPagination pagination_class = StandardResultsSetPagination
permission_classes = [permissions.AllowAny] permission_classes = [permissions.AllowAny]
queryset = Album.objects.cache().all()
def get_queryset(self):
return Album.objects.all()
class RetrieveUpdateDestroyAlbumAPIView( class RetrieveUpdateDestroyAlbumAPIView(
@ -284,15 +282,14 @@ class RetrieveUpdateDestroyAlbumAPIView(
lookup_url_kwarg = "slug" lookup_url_kwarg = "slug"
permission_classes = [IsAdminOrReadOnly] permission_classes = [IsAdminOrReadOnly]
serializer_class = FullAlbumSerializer serializer_class = FullAlbumSerializer
queryset = Album.objects.cache().all()
class ListAuthorsAPIView(generics.ListAPIView): class ListAuthorsAPIView(generics.ListAPIView):
serializer_class = AuthorSerializer serializer_class = AuthorSerializer
pagination_class = StandardResultsSetPagination pagination_class = StandardResultsSetPagination
permission_classes = [permissions.AllowAny] permission_classes = [permissions.AllowAny]
queryset = Author.objects.cache().all()
def get_queryset(self):
return Author.objects.all()
class RetrieveUpdateDestroyAuthorAPIView( class RetrieveUpdateDestroyAuthorAPIView(
@ -302,6 +299,7 @@ class RetrieveUpdateDestroyAuthorAPIView(
lookup_url_kwarg = "slug" lookup_url_kwarg = "slug"
permission_classes = [IsAdminOrReadOnly] permission_classes = [IsAdminOrReadOnly]
serializer_class = FullAuthorSerializer serializer_class = FullAuthorSerializer
queryset = Author.objects.cache().all()
class ListenSongAPIView(generics.GenericAPIView): class ListenSongAPIView(generics.GenericAPIView):