How to Resolve HttpRequest AssertionError in Django Friend Request View?

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 ofdjango.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.

New contributor

Previn Bogopa is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật