citusdata / citusdata/django-multitenant

Possible for users to run queries against a different tenant_id?

Open
#100 1 comment 1 reaction 0 assignees View on GitHub
help wanted
Dominant language
Python
Stars
823
Forks
126
PR merge metrics
No merged PRs in 30d

Description

I have the model design below. Here is the logic at the heart of my question:
- teams can access "marketplace" courses that are available to all teams
- teams can create their own courses that are private to them

```
class Team(TenantModel):
tenant_id = 'id'
name = models.CharField(max_length=50)

class User(TenantModelMixin, AbstractBaseUser, PermissionsMixin):
team = models.ForeignKey(Team, on_delete=models.PROTECT)
tenant_id = 'team_id'
email = models.EmailField(max_length=100, unique=True, db_index=True)
# .... additional fields

class Course(TenantModel):
team = models.ForeignKey(Team, on_delete=models.PROTECT)
tenant_id = 'team_id'
title = models.CharField(max_length=200)
# .... additional fields
```

The middleware:

```
class MultitenantMiddleware:
def __init__(self, get_response):
self.get_response = get_response

def __call__(self, request):
if request.user and not request.user.is_anonymous:
set_current_tenant(request.user.team)
return self.get_response(request)
```

What is the best way to structure the DB and/or middleware to support this use case?

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.