Open andrewtmendoza opened 3 years ago
Actually, support for many to many relationships isn't intended (so far), so whatever works is more of a coincidence than a feature :(
I'm late to the show but you can just do this by:
objects = ScopedManager(tenant="tenants", _manager_class=MyUserManager)
And add a middleware:
class SetTenantMiddleware:
def __init__(self, get_response):
self.get_response = get_response
def __call__(self, request):
if request.user.is_authenticated:
if request.path.startswith("/admin_panel/"):
# no scoping in admin
with scopes_disabled():
response = self.get_response(request)
else:
with scope(tenant=request.user.tenant_id):
response = self.get_response(request)
else:
response = self.get_response(request)
return response
In case anyone else would encounter the same problem.
Hi @raphaelm - I'm working on a multi-tenancy app in which there is a many-to-many relationship between users and tenants.
Is this the correct way to represent it in django-scopes?
models.py
middleware.py
It seems to work most of the time, although I occasionally see a strange error with the User ModelForm where the Sponsor model data is retrieved from the incorrect tenant.