Django many-to-many filtering gives "No operator matches the given name and argument types."

I have the following models in my Django project:

model Profile(models.Model):
    user = models.OneToOneField(User)
    faculties = models.ManyToManyField('users.Faculty', related_name='profiles')

Now I want to define a function that gives me all coworkers of a user/profile like so:

def coworkers(self):
    coworkers = User.objects.none()
    for faculty in self.faculties.all():
        coworkers |= User.objects.filter(Q(profile__faculties__id__exact=faculty.id))
    return coworkers

But then I always run in a ProgrammingError at /exercise/ operator does not exist: character varying = integer or No operator matches the given name and argument types. You might need to add explicit type casts. ) was the direct cause of the following exception:.

I also tried:

faculties = [f.id for f in self.faculties.all()]
coworkers = User.objects.filter(Q(profile__faculties__id__in=faculties)

Both versions also do not work with __exact instead of __in.

Any help?

Back to Top