Home > Net >  Django ManytoManyField query does not exist
Django ManytoManyField query does not exist

Time:06-11

I've created models for the logic of friend list and friend request. In the FriendRequest I've defined a method which adds User relation to FriendList if accept method is called. However I'm unable to do so because of the error shown below. I don't understand the error as I'm adding the User in the model.

views.py

friend_request = FriendRequest.objects.get(id=request_id)

if request_option == 'accept':
  friend_request.accept()
if request_option == 'decline':
  friend_request.decline()

models.py

class FriendList(models.Model):
    user    = models.OneToOneField(User, on_delete=models.CASCADE, related_name="user")
    friends = models.ManyToManyField(User, blank=True, related_name="friends") 

    def add_friend(self, account):
        if not account in self.friends.all():
            self.friends.add(account)
            self.save()


class FriendRequest(models.Model):
    sender = models.ForeignKey(User, on_delete=models.CASCADE, related_name="sender")
    receiver = models.ForeignKey(User, on_delete=models.CASCADE, related_name="receiver")
    is_active = models.BooleanField(blank=False, null=False, default=True)
    timestamp = models.DateTimeField(auto_now_add=True)

    def accept(self):
        receiver_friend_list = FriendList.objects.get(user=self.receiver)
        if receiver_friend_list:
            receiver_friend_list.add_friend(self.sender)
            sender_friend_list = FriendList.objects.get(user=self.sender)
            if sender_friend_list:
                sender_friend_list.add_friend(self.receiver)
                self.is_active = False
                self.save()

    def decline(self):
        self.is_active = False
        self.save()
 File "C:\Users\models.py", line 77, in accept
    receiver_friend_list = FriendList.objects.get(user=self.receiver)
  File "C:\Python310\lib\site-packages\django\db\models\manager.py", line 85, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "C:\Python310\lib\site-packages\django\db\models\query.py", line 496, in get
    raise self.model.DoesNotExist(
friends.models.FriendList.DoesNotExist: FriendList matching query does not exist.

CodePudding user response:

You are querying for a non-existing object. To avoid that error use get_or_create:

def accept(self):
    receiver_friend_list, created = FriendList.objects.get_or_create(user=self.receiver)
    if receiver_friend_list:
        receiver_friend_list.add_friend(self.sender)
        sender_friend_list = FriendList.objects.get(user=self.sender)
        if sender_friend_list:
            sender_friend_list.add_friend(self.receiver)
            self.is_active = False
            self.save()

This is what Django Docs shows and there is a pythonic way also taken from Django Docs.

try:
    receiver_friend_list= FriendList.objects.get(user=self.receiver)
except FriendList.DoesNotExist:
    receiver_friend_list = FriendList(user=self.receiver)
    receiver_friend_list.save()
  • Related