class CustomUserViewSet(UserViewSet):
permission_classes = (IsAuthenticatedOrReadOnly,)


@action( 
    detail=False, 
    methods=("get",), 
    permission_classes=(IsAuthenticated,), 
) 
def subscriptions(self, request): 
    """Список авторов, на которых подписан пользователь.""" 
    user = self.request.user 
    queryset = user.followers.exclude(id=user.id) 
    pages = self.paginate_queryset(queryset) 
    serializer = SubscriptionSerializer( 
        pages, many=True, context={"request": request} 
    ) 
    return self.get_paginated_response(serializer.data) 

@action( 
    detail=True, 
    methods=("post",), 
) 
def subscribe(self, request, id=None): 
    """Подписка на автора.""" 
    user = self.request.user 
    author = get_object_or_404(User, pk=id) 

    if self.request.method == "POST": 
        if Subscription.objects.filter(user=user, author=author).exists(): 
            return Response( 
                {"errors": "Подписка уже оформлена!"}, 
                status=status.HTTP_400_BAD_REQUEST, 
            ) 

        queryset = Subscription.objects.create(author=author, user=user) 
        serializer = SubscriptionSerializer( 
            queryset, context={"request": request} 
        ) 
        return Response(serializer.data, status=status.HTTP_201_CREATED) 

    return Response(status=status.HTTP_405_METHOD_NOT_ALLOWED) 

@subscribe.mapping.delete 
def unsubscribe(self, request, id=None): 
    """Отписка от автора.""" 
    user = self.request.user 
    author = get_object_or_404(User, pk=id) 

    if not Subscription.objects.filter(user=user, author=author).exists(): 
        return Response( 
            {"errors": "Вы уже отписаны!"}, 
            status=status.HTTP_400_BAD_REQUEST, 
        ) 

    subscription = get_object_or_404( 
        Subscription, user=user, author=author) 
    subscription.delete() 

    return Response(status=status.HTTP_204_NO_CONTENT) 
class SubscriptionSerializer(CustomUserSerializer, PageNumberPagination):
"""Подписка."""

email = serializers.ReadOnlyField(source="author.email")
id = serializers.ReadOnlyField(source="author.id")
username = serializers.ReadOnlyField(source="author.username")
first_name = serializers.ReadOnlyField(source="author.first_name")
last_name = serializers.ReadOnlyField(source="author.last_name")
recipes = serializers.SerializerMethodField()
recipes_count = serializers.ReadOnlyField(source="author.recipes.count")
is_subscribed = serializers.SerializerMethodField()

class Meta:
    model = Subscription
    fields = (
        "id",
        "username",
        "first_name",
        "last_name",
        "email",
        "is_subscribed",
        "recipes",
        "recipes_count",
    )

def get_recipes_count(self, obj):
    """Количество рецептов автора."""
    return Recipe.objects.filter(author=obj.id).count()

def get_recipes(self, obj):
    author_recipes = obj.author.recipes.all()

    if author_recipes:
        serializer = ShortRecipeSerializer(
            author_recipes,
            context={"request": self.context.get("request")},
            many=True,
        )
        return serializer.data

    return []

def validate(self, data):
    user = self.context["request"].user
    author = self.instance.author if self.instance else data["author"]

    if user == author:
        raise serializers.ValidationError(
            "Нельзя подписаться или отписаться от себя!")

    return data
перенеси все проверки в serializer не нарушив логику работы