Модель подписки
class Follow(models.Model):
user = models.ForeignKey(
User,
related_name='follower',
on_delete=models.CASCADE
)
author = models.ForeignKey(
User,
related_name='following',
on_delete=models.CASCADE
)
сериализатор с методом
class UserFollowSerializer(serializers.ModelSerializer):
is_subscribed = serializers.SerializerMethodField()
recipes = UserRecipeSerializer(many=True, read_only=True)
recipes_count = serializers.SerializerMethodField()
class Meta:
model = User
fields = (
'email',
'id',
'username',
'first_name',
'last_name',
'is_subscribed',
'recipes',
'recipes_count'
)
def get_is_subscribed(self, obj):
return obj.following.filter(user=self.context['user']).exists()