I was wondering if anyone knows any way to manipulate queryset before it is compiled? Similar to that of sqlalchemy .
My goal is to get information from a tenant connected to the system, but overwriting the
get_queryset method in
Manager class is not enough.
My solution so far is creating a customizable lookup.
@Field.register_lookup class CurrentTenant(models.Lookup): lookup_name = "ct" def as_sql(self, compiler, connection): lhs, lhs_params = self.process_lhs(compiler, connection) tenant = get_current_tenant() params = lhs_params return "%s = '%s'" % (lhs, tenant.id), params class TenantManager(models.Manager): def get_queryset(self): return super().get_queryset().filter(tenant__id__ct=FAKE_FILTER)