def get(self, request, format=None, pk=None): following_user = request.user to_be_followed_user = get_object_or_404(models.User, pk=pk) # Create a notification for the post owner if not following_user in to_be_followed_user.followers.all(): notifications = Notification.objects.filter(user_tx=following_user) notified = False for notification in notifications: if notification.user_rx == to_be_followed_user and notification._type == 'follow': notified = True if not notified: # Create notification if ine hasn't been made already notification = Notification() notification._type = 'follow' # I am using rx to mean receiver and tx to mean transceiver notification.user_rx = to_be_followed_user notification.user_tx = following_user notification.save() if following_user.is_authenticated: if following_user in to_be_followed_user.followers.all(): to_be_followed_user.followers.remove(following_user) following_user.following.remove(to_be_followed_user) following = False else: to_be_followed_user.followers.add(following_user) following_user.following.add(to_be_followed_user) following = True updated = True data = {'following': following, 'updated': updated} return Response(data)