Django queryset filter check if ManyToMany Field contains specific object

I have these two models :

class Project(models.Model):
    name = models.CharField(max_length=50)
    users = models.ManyToManyField(User)

class User(models.Model):
    name = models.CharField(max_length=25)

I want to get a queryset containing all the Projects with a specific user in the 'users' ManyToManyField

I tried this : Project.objects.filter(users__contains=user), but it's not working

Does someone know how can I do it ?

if filtering with id:

Project.objects.filter(users=search_id)

if filtering with user name:

Project.objects.filter(users__name__icontains=user.name)
Back to Top