def filter_by_related(self, *args, **kwargs):
        objects = {}
        for obj in args:
            ctype = ContentType.objects.get_for_model(obj)
            if ctype not in objects:
                objects[ctype] = []
            objects[ctype].append(obj)

        data = self.all()
        for ctype, objs in objects.items():
            ids = [obj.pk for obj in objs]
            if kwargs.get('inverse', False):
                data = data.filter(
                    relationships__to_content_type=ctype,
                    relationships__to_object_id__in=ids,
                )
            else:
                data = data.filter(
                    inverserelationships__from_content_type=ctype,
                    inverserelationships__from_object_id__in=ids,
                )
        return data
dataset/ETHPy150Open wq/wq.db/patterns/relate/models.py/RelatedModelManager.filter_by_related/original