Assistance Needed with Multi-Tenancy in Django Views

Hello,

Before describing the issue, let me briefly explain the architecture of my project. I am building an app that supports multi-tenancy in Django, using a PostgreSQL database. I have decided not to use different schemas for each tenant. Instead, I store all tenant data in the same schema and the same table but distinguish the data with a tenant_id. I have designed the entire database architecture and created all the models.

Problem: I am currently stuck on the view part. When I write the logic, I need to pass the tenant ID every time to fetch the data from the model. Is there a way to pass the tenant ID just once, and have the model automatically filter the data accordingly?

Are you using some sort of external django-package for this?
If not, I recommended some here, just a few minutes ago.

thank you for your support,
but i need the way to implement the logic in view.py i don’t want to pass the “tenant id ” every time in function of views.py , when do query work.

Then pass the tenant id with every request.

The model could possibly be handling hundreds of requests from different tenants at the same time. There’s no practical way for the model to know which tenant is going to be requested for any individual query.

1 Like

thank you
ken white sell for your suggestion.i hope this will work with me

Using a tenant ID in jQuery can indeed pose security risks . how i will handled any suggestion on this.

I’m sorry, I phrased my response very poorly. I meant to say that you need to pass the tenant id from the view to the query with every query being made.

I know that that’s not what you would like to do, but you don’t really have any other good or reliable options.