As mentioned in the thread: django multi tenancy issue , thread local does not work in async. Thus if feels like a suboptimal solution and not fully compatible with Django.
this solution does not seem sub optimal to me. if the library doesn’t work with async, and using async is a requirement, then make a pr or fork the code to use contextvars to handle the magic.
It should not be that hard to enforce this at the modelmanager level ?
It seems challenging to achieve your goal because Model.objects
is instantiated once in the global namespace. Instead, you could make a class method that returns a queryset that applies your filter.
class Obj(models.Model):
@classmethod
def tenant_objects(cls, tenant):
return cls.objects.filter(tenant=tenant)
Obj.tenant_objects(tenant="public").all()