Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django queryset excluding many to many objects

Let's assume we have a model:

 class a(models.Model):
     users = models.ManyToManyField(User) # django.contrib.auth.models.User

and these variables:

user = request.user
queryset = a.objects.all()

Then I want to exclude these records from a model that contains the user in users. How can I do that?

queryset.exclude(...)
like image 767
aemdy Avatar asked Dec 27 '22 13:12

aemdy


1 Answers

It's as simple as this:

queryset.exclude(users=user)
like image 148
Daniel Roseman Avatar answered Jan 10 '23 04:01

Daniel Roseman