0

Am trying to enable my website users block other users they don't want again as friends just like we can on Facebook. I decided to implement it with Django guardian. when a user clicks a button, a view(block_user_view) is called and the profile the user wants to block is added to group which is assigned a custom permission i created(cant_view_profile). But i always get this error "permission matching query doesn't exist". I can assign these permissions from the admin panel but it runs into error when i try it from my views. I don't know of other ways to actualize this functionality with Django.

this is my view

def block_user_view(request, id):
    if request.method == "POST":
       grouped = Group.objects.get(name="blockedusers")
       friend = Profile.objects.get(id = id)
       user = request.user
       assign_perm("cant_view_profile", grouped, user)
       friend.groups.add(grouped)
       print(friend.has_perm("cant_view_profile", user))

    return render(request, "profiles/blockuser.html", {})

my model

class Profile(models.Model):
    user = models.OneToOneField(User, on_delete= models.CASCADE)
    prof_pics = models.ImageField(null = True, blank =True, upload_to= 'images/')
    friends = models.ManyToManyField(User, blank=True, related_name="friend" )
    bio = models.TextField(blank= True)
    group = models.ManyToManyField(Group, blank=True,)
 


    def __str__(self):
        return str(self.user)

    
    class Meta:
        permissions = [("cant_view_profile","cant view profile")]
  • `Group.objects.get(name="blockedusers")` Is attempting to get a single object with the name blocked users. Maybe you want `Group.objects.filter(name="blockedusers")`? Post your `models.py` if you want help. – Anthony Jun 14 '22 at 09:13
  • Edit your original question, don't post your models in comments. – Anthony Jun 14 '22 at 09:47

0 Answers0