updated serializer

This commit is contained in:
Alexander Karpov 2023-12-08 16:35:32 +03:00
parent debdf53927
commit 63311d1d26

View File

@ -32,10 +32,12 @@ class SongSerializer(serializers.ModelSerializer):
@extend_schema_field(serializers.BooleanField) @extend_schema_field(serializers.BooleanField)
def get_liked(self, obj): def get_liked(self, obj):
if self.context["request"].user.is_authenticated: if "request" in self.context:
return SongUserRating.objects.filter( if self.context["request"].user.is_authenticated:
song=obj, user=self.context["request"].user, like=True return SongUserRating.objects.filter(
).exists() song=obj, user=self.context["request"].user, like=True
).exists()
return None
class Meta: class Meta:
model = Song model = Song
@ -66,7 +68,9 @@ class ListSongSerializer(SetUserModelSerializer):
def get_liked(self, obj): def get_liked(self, obj):
if "likes" in self.context: if "likes" in self.context:
return self.context["likes"] return self.context["likes"]
return obj.id in self.context["likes_ids"] if "likes_ids" in self.context:
return obj.id in self.context["likes_ids"]
return None
class Meta: class Meta:
model = Song model = Song