0

I understand how to handle several databases through routers, however, how would it be handled when I have two databases with the same structure but it is required to save the information according to the database chosen by the user when starting a session . I have a session login with its corresponding username and password, in addition to that it is mandatory that the user choose a database through a selector to start the session to the application correctly, this information on the name of the selected database is moves through a session cookie for the entire session (forgive the redundancy), all the operations of the application must be done on the database that was initially selected in the access login, how to correctly route the databases for this case?

DATABASES = {
    'default' : {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': env.str('NAME_DB_ZF'),
        'USER': env.str('USER_DB'),
        'PASSWORD': env.str('PASS_DB'),
        'HOST': 'localhost',
        'PORT': '3306',
    },
    'SBOJOZF': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': env.str('NAME_DB_ZF'),
        'USER': env.str('USER_DB'),
        'PASSWORD': env.str('PASS_DB'),
        'HOST': 'localhost',
        'PORT': '3306',
    },
    'SBOJOCOL': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': env.str('NAME_DB_COL'),
        'USER': env.str('USER_DB'),
        'PASSWORD': env.str('PASS_DB'),       
        'HOST': 'localhost',
        'PORT': '3306',
    }
}
  • Does this answer your question? [Django Database routing based on current user logged in](https://stackoverflow.com/questions/39354715/django-database-routing-based-on-current-user-logged-in) – Abdul Aziz Barkat Jun 28 '22 at 13:57

1 Answers1

0

I am going to assume you are asking about routing to multiple databases, forgive me if that was not your question. I think a converter is what you need.

In your urls.py:

from django.urls import path, register_converter

register_converter(convertors.DatabaseNameConverter, 'db_name')

urlpatterns = [
    path('<db_name:db_name>/', views.example_view),
]

Then in convertors.py:

from django.conf import settings


class DatabaseNameConverter:
    """
    Regex converter for URL that takes all available names of databases for routing.
    """
    regex = '('+')|('.join(settings.DATABASES.keys())+')'

    def to_python(self, value):
        return str(value)

    def to_url(self, value):
        return str(value)

Then this is just a query parameter you can use in your view, here is an example view:

def example_view(request, db_name):
    users = User.objects.using(db_name).all()
    # Other code

Alternatively you can check which database you are using to know how to query: db_name == "default"

Example for delete:

instance = User.objects.using(db_name).get(id=id)
instance.delete()
Sorin Burghiu
  • 715
  • 1
  • 7
  • 26
  • I understand, and the last line where you declare the queryset works for me, how would you do it for when you save, update and delete records? – Sebastian Narvaez Jun 28 '22 at 12:34
  • You can simply change the query to what you need to, I chose a select query as an example. For updating you would use `object.save(using='the_db')`. I added an example for delete to the answer^ – Sorin Burghiu Jun 28 '22 at 13:27
  • It still doesn't work, how would I correctly implement the routers for my case? – Sebastian Narvaez Jun 28 '22 at 19:35