You can quickly build a multi-tenant site in Django with the help of this nifty package django-tenant-schemas. It works by assigning each tenant into its own database schema and having one
public schema for anything that needs to be shared.
It wraps Django's
connection with its own wrapper that help to route connection request accordingly based on the hostname
tenant1.domain.com to schema
tenant1. So this works great when you're in the context of Django application.
I have Celery task that needs to be run and needs to access the database. I could just pass the model directly to the Celery task params but it is not advisable to do so.
def get(self, request, format=None):
site = Site.objects.first()
and in my
tasks.py, I have
def run_report(title, site_id):
# we need this
# c = connection.cursor()
site = Site.objects.get(pk=site_id)
If I don't have the commented lines, it won't work because it is hitting
public schema and the tables do not exist in
Note: We can use
set_schema because that was the function available in the wrapper that was used internally by the package to handle switching of the schema.