[Answered ]-Show only users that don't yet exist in the through table

1👍

You can get all users that do not yet belong to the request.user’s company with:

users_in_company = request.user.company.user_set.all().values(pk).distinct()
users_not_in_company = User.objects.all().exclude(pk__in=list(users_in_company))

Bonus:
If you want to get all users which don’t belong to any company:

users_not_in_any_company = users_not_in_company.filter(company__isnull=True) # Equal to User.objects.filter(company__isnull=True), but it doesn't really matter.

Note:

request.user.company.user_set.all()

If you changed the related name of the user to the company, change user_set too ofcourse. I could give you the full example code, but alas, no models.

Edit:

To get all users in a company if the request.user has one:

users = request.user.company.user_set.all()

To filter those users which belong to a specific project:

users.filter(project=my_project)

To get users that don’t belong to the project:

users = User.objects.all().exclude(project__pk=self.kwargs['pk'])

To get users that belong to the request.user‘s company, but not to a project:

request.user.company.user_set.all().exclude(project__pk=self.kwargs['pk'])

Final

User.objects.filter(company=self.request.user.company).exclude(project_users=project_pk)

Leave a comment