When I started building Label Mechanics, one of the first things I had to figure out was how to handle multiple record labels using the same application.
At first, it didn't seem particularly complicated.
A user creates a label. The label has releases, orders, transactions and other data. Simple enough.
Except I also wanted users to be able to work with more than one label.
And I definitely didn't want someone managing Label A to accidentally see the sales or financial data belonging to Label B.
So, before building anything particularly interesting, I needed to make some decisions about multi-tenancy.
This isn't a tutorial on building the perfect multi-tenant architecture. It's a look at what I've implemented so far in Django and Django REST Framework, why I made certain choices, and what still needs work.
First, what exactly is a tenant?
In Label Mechanics, a tenant is a record label.
The application is being built for independent labels that need to manage releases, costs, sales and eventually marketing results.
Each label has its own data.
But the relationship between users and labels isn't necessarily one-to-one.
Someone might run two small labels. Another person might help with marketing for several labels. And a label might have multiple people working on it.
That meant attaching a single label field directly to the User model wasn't going to be enough.
Instead, I went with three models:
UserLabelMembership
The important part is Membership.
Why I added a Membership model
The current implementation is fairly straightforward:
class Membership(models.Model): user = models.ForeignKey( User, on_delete=models.CASCADE, related_name="memberships" ) label = models.ForeignKey( Label, null=True, blank=True, on_delete=models.CASCADE, related_name="memberships" ) role = models.CharField( max_length=25, choices=UserRole.choices, default=UserRole.LABEL_OWNER ) class Meta: constraints = [ models.UniqueConstraint( fields=["user", "label"], name="unique_user_label_membership" ) ] A user can belong to multiple labels, and a label can have multiple users.
The membership also stores a role.
Currently, I've defined three roles: label owner, label member and marketing manager.
I haven't built a complete role-based permission system around them yet. For now, the important part is knowing which labels a user belongs to.
One detail I still need to revisit is allowing label to be null in the membership model. For an actual membership, that doesn't seem particularly useful, and it complicates the uniqueness constraint.
It's the kind of small decision that's easy to leave in place while you're working on other parts of the application.
One database, multiple labels
I'm using PostgreSQL, with all labels sharing the same database and tables.
I didn't create separate databases or schemas for each label.
Instead, the data is connected to the appropriate label through foreign keys.
For example, the Release model includes:
class Release(models.Model): label = models.ForeignKey( Label, on_delete=models.CASCADE, related_name="releases" ) title = models.CharField(max_length=350) artist = models.CharField(max_length=200) catalog_number = models.CharField(max_length=50) That means the application needs to enforce the separation between labels.
For the current stage of Label Mechanics, this approach seemed reasonable. It's relatively simple, works with the Django ORM, and doesn't introduce infrastructure complexity I don't currently need.
But it also means I have to be careful with queries.
A missing filter can become a data access problem.
Filtering data in Django REST Framework
This is where DRF makes things fairly convenient.
For example, the current ReleaseViewSet looks like this:
class ReleaseViewSet(viewsets.ModelViewSet): serializer_class = ReleaseSerializer permission_classes = [IsAuthenticated] def get_queryset(self): return Release.objects.filter( label__memberships__user=self.request.user ).distinct() The idea is simple.
When a user requests releases, Django only returns releases belonging to labels where that user has a membership.
The same pattern appears elsewhere in the application.
For release formats, the query goes through the release:
def get_queryset(self): return ReleaseFormat.objects.filter( release__label__memberships__user=self.request.user ).distinct() For order items, it goes through the order:
def get_queryset(self): return OrderItem.objects.filter( order__label__memberships__user=self.request.user ).distinct() I like this approach because the relationship is visible in the query itself.
But there's an obvious downside.
The more models I add, the more places I have to remember to apply the correct filtering.
And remembering to do something everywhere isn't exactly a great security strategy.
At some point, I may need to move more of this logic into reusable components.
For now, I'm keeping it explicit while I work through the domain.
Filtering isn't the whole story
One thing I had to think about was the difference between reading data and creating it.
Filtering the queryset means a user shouldn't be able to retrieve another label's releases through that endpoint.
But what happens when someone sends a POST request containing a different label's ID?
That's why I added validation to the serializers.
For example:
def validate_label(self, value): if not Membership.objects.filter( user=self.context["request"].user, label=value ).exists(): raise serializers.ValidationError( "You do not have access to this label." ) return value This checks whether the authenticated user belongs to the label they're trying to assign.
Similar checks exist for releases, contacts, orders and other relationships.
There's another complication, though.
Some models reference more than one object. An order, for example, can have a contact. An order item can reference a release format.
Those relationships need to make sense within the same label.
Otherwise, you could end up with an order belonging to one label and an item pointing to a release belonging to another.
I've added some cross-label validation for these cases, but this is an area I still want to review more carefully, especially when updating existing objects.
The current code is a working implementation, not a guarantee that every possible access path has been covered.
Testing whether labels can see each other's data
I also started adding API tests for tenant isolation.
One of them creates two users, two labels and two releases.
Each user belongs to a different label.
Then it authenticates as the first user and requests the release list.
The important assertions are:
self.assertEqual( response.status_code, status.HTTP_200_OK, ) self.assertEqual(len(response.data), 1) self.assertEqual( response.data[0]["id"], self.release_a.id, ) It's a basic test, but it checks something important: the endpoint only returns the release belonging to the authenticated user's label.
Of course, one test doesn't prove that the entire application is properly isolated.
I still need more coverage for detail endpoints, updates, related objects and different membership roles.
And because the project now includes economics, campaigns and integrations, tenant isolation has to remain part of the development process rather than something I check once and forget.
What I would probably do differently
Looking at the code now, there are a few things I'd like to improve.
First, I'd make tenant-related validation more consistent. Some checks are currently repeated across serializers, which works but creates more places for mistakes.
Second, I'd expand the tests before adding too many more endpoints.
Third, I'd take a closer look at the role system. Having label_owner, label_member and marketing_manager in the database doesn't automatically mean the application enforces different permissions for them.
That's a separate problem.
And finally, I need to be careful not to confuse an authenticated user with an authorized user.
IsAuthenticated answers one question.
Whether that user should be allowed to access or modify a particular label's data is another.
Why I'm keeping it relatively simple
I could probably spend weeks researching different multi-tenancy architectures.
Separate databases. PostgreSQL schemas. Third-party Django packages. More complicated permission systems.
Some of those approaches might make sense later.
But Label Mechanics is still an early-stage project, and I'm not even sure yet how many independent labels would want to use it.
Building infrastructure for thousands of customers before finding the first few would be a strange use of time.
That doesn't mean security can wait.
It means I'm trying to keep the architecture understandable while making sure the basic isolation rules are implemented and tested.
There's a difference between avoiding unnecessary complexity and ignoring problems.
I'm trying to stay on the right side of that line.
What comes next?
The backend has grown beyond the initial release catalog.
I've been working on release economics, orders, transactions, marketing campaigns and an early Shopify integration.
Each new part introduces more relationships between models, and those relationships make tenant isolation more important.
So the next step isn't necessarily adding another feature.
It's making sure the existing pieces behave correctly together.
I'm also using ChatGPT throughout the development process, including reviewing architecture and discussing implementation choices. It helps me work through unfamiliar problems, but I still need to understand the code and verify that the suggested solutions actually work.
That's especially true for permissions and data isolation.
AI can suggest a queryset.
It can't make an untested assumption safe.
And that's probably one of the more useful lessons I'm taking from building Label Mechanics so far.
Multi-tenancy isn't just about adding a label_id to your models.
It's about making sure that relationship is respected everywhere the data can be accessed or changed.
I'm not finished with that part yet.
But at least I now have a much clearer idea of what needs to be checked before I can call it finished.
I'm documenting the development of Label Mechanics, a SaaS project for independent record labels. The first article covers the product idea and the question of whether small labels actually need another piece of software.

