I’m working on a Django project where users can send and accept friend requests. When a friend request is accepted, I want to add the sender and receiver to each other’s friend list and send a notification. However, I’m encountering an error when trying to send the notification.
Problem Description:
When a user accepts a friend request, I want to add the sender to the receiver’s friends list and vice versa. However, I get the following error:
AssertionError: The
requestargument must be an instance of
django.http.HttpRequest, not
userauths.models.User. [28/May/2024 16:09:33] "GET /accept-friend-request/?id=8 HTTP/1.1" 500 121207
so let me provide the link api to the function
path("accept-friend-request/", views.accept_friend_request, name="accept-friend-request"),
let me provide the accept_friend_request function
@api_view(['GET'])
@login_required
def accept_friend_request(request):
if request.method == 'GET':
sender_id = request.GET.get('id')
receiver = request.user
# Log the sender_id
logger.debug(f"Sender ID: {sender_id}")
try:
# Retrieve the sender user from the database
sender = User.objects.get(id=sender_id)
# Retrieve the sender's profile
sender_profile = sender.profile
# Add sender's profile to receiver's friends and vice versa
receiver.profile.friends.add(sender_profile)
sender_profile.friends.add(receiver.profile)
# Delete the friend request
friend_request = FriendRequest.objects.filter(receiver=receiver, sender=sender).first()
friend_request.delete()
# Send notification
send_notification(sender, receiver, None, None, noti_friend_request_accepted)
data = {
"message": "Accepted",
"bool": True,
}
return JsonResponse({'data': data})
except User.DoesNotExist:
logger.error(f"User with ID {sender_id} does not exist.")
return JsonResponse({'error': 'User does not exist.'}, status=404)
else:
return JsonResponse({'error': 'Only GET requests are allowed.'})
Here is my FriendRequest Model
class FriendRequest(models.Model):
fid = ShortUUIDField(length=7, max_length=25, alphabet="abcdefghijklmnopqrstuvxyz123")
sender = models.ForeignKey(User, on_delete=models.CASCADE, null=True, blank=True, related_name="request_sender")
receiver = models.ForeignKey(User, on_delete=models.CASCADE, null=True, blank=True, related_name="request_receiver")
status = models.CharField(max_length=10, default="pending", choices=FRIEND_REQUEST)
date = models.DateTimeField(auto_now_add=True)
def save(self, *args, **kwargs):
super().save(*args, **kwargs)
if self.status == "accepted":
sender_profile = self.sender.profile
receiver_profile = self.receiver.profile
receiver_profile.friends.add(sender_profile)
sender_profile.friends.add(receiver_profile)
def __str__(self):
return f"Friend request from {self.sender} to {self.receiver}"
class Meta:
ordering = ["-date"]
verbose_name_plural = "Friend Requests"
Question:
How can I resolve the AssertionError related to HttpRequest in this context?
Any help or guidance would be greatly appreciated!
What I Tried:
Logging: I added logging to ensure that the sender_id is being retrieved correctly, which it is.
Profile Retrieval: I confirmed that the sender.profile and receiver.profile are being accessed correctly.
Database Check: I confirmed that both the user and the friend request exist in the database.
Expected Behavior:
I expect the friend request to be accepted, the users to be added to each other’s friend list, and a notification to be sent without causing an AssertionError.
Previn Bogopa is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.