django multi databases and multi sessions

60 views
Skip to first unread message

WM

unread,
Mar 20, 2023, 11:08:41 AM3/20/23
to Django users
Hi,

I would like to have your help. 

Using Django multiple databases in a project, can I create multiple sessions in the project? If so, I would like to know how to. For example, three databases with two different login functions; The steps are followed: 

In a webpage, login as a homepage member(using db A), then at a certain menu among homepage menus, the homepage login user is required to enter new login id with new password to explore another world of the website(using db B & C). I don't know having multiple sessions in one project with multiple databases is possible or not. If possible, I appreciate any advise how to do. Thank you. 

Sebastian Jung

unread,
Mar 20, 2023, 4:59:32 PM3/20/23
to django...@googlegroups.com
I think you mean multi tenancy... There are packages for that. Google after django multi tenant..


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/f00f3d69-51ba-4e19-b8b2-c02375cd1504n%40googlegroups.com.

Dan Wood

unread,
Mar 20, 2023, 6:00:39 PM3/20/23
to django...@googlegroups.com
I’m working on a big multi tenant app feel free to email for information, I use a different schema for each tenant 

Ujjwal Joshi

unread,
Mar 21, 2023, 8:49:49 AM3/21/23
to django...@googlegroups.com
There is a package for multi-tenancy in django . Also there is another package called django-tenant-users which you can use to manage permission between tenants. If you want to create a multi tenant on your own you can research on the internet. There is tons of information about implementing multi-tenancy online.

--

Jd Mehra

unread,
Mar 24, 2023, 8:41:32 AM3/24/23
to Django users
You need to write a database router like this one:
class MyModelRouter:
    def db_for_read(self, model, **hints):
        if model._meta.app_label == 'myapp':
            return 'second_db'
        return None

    def db_for_write(self, model, **hints):
        if model._meta.app_label == 'myapp':
            return 'second_db'
        return None

    def allow_relation(self, obj1, obj2, **hints):
        if obj1._meta.app_label == 'myapp' or obj2._meta.app_label == 'myapp':
            return True
        return None

    def allow_migrate(self, db, app_label, model_name=None, **hints):
        if app_label == 'myapp':
            return db == 'second_db'
        return None
Reply all
Reply to author
Forward
0 new messages