def follow(self, request, pk=None): profile = self.get_user(request, pk) allow_follow_user(request.user, profile) profile_followers = profile.followers with transaction.atomic(): if request.user.is_following(profile): request.user.follows.remove(profile) followed = False profile_followers -= 1 profile.followers = F('followers') - 1 request.user.following = F('following') - 1 else: request.user.follows.add(profile) followed = True profile_followers += 1 profile.followers = F('followers') + 1 request.user.following = F('following') + 1 profile.save(update_fields=['followers']) request.user.save(update_fields=['following']) return Response({ 'is_followed': followed, 'followers': profile_followers, })
def follow(self, request, pk=None): profile = self.get_user(request, pk) allow_follow_user(request.user, profile) profile_followers = profile.followers with transaction.atomic(): if request.user.is_following(profile): request.user.follows.remove(profile) followed = False profile_followers -= 1 profile.followers = F('followers') - 1 request.user.following = F('following') - 1 else: request.user.follows.add(profile) followed = True profile_followers += 1 profile.followers = F('followers') + 1 request.user.following = F('following') + 1 profile.save(update_fields=['followers']) request.user.save(update_fields=['following']) return Response({'is_followed': followed, 'followers': profile_followers})