Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8609499
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T03:53:38+00:00 2026-06-12T03:53:38+00:00

I am rewriting a legacy application that has a database for each customer. Each

  • 0

I am rewriting a legacy application that has a database for each customer. Each customer has its own authentication and user set. Thus, I’ll need a custom authentication backend because django’s auth is set to only use default. I have written middleware that examines the url upon every request and extracts information there to set a database_name on the request.

If I had access to the request during processing of my custom authencation backend, I could easily perform database calls as user = User.objects.using(request.db).get(username=username) However, I see no easy way to accomplish this. I’ve seen an answer to that here: Access request.session from backend.get_user, but this wouldn’t appear to be thread safe so I don’t want to go down that road.

The only solution I can see that still uses django-auth is to have an authentication backend for each customer that sets the database name to be used as a class attribute. Then, I would create a custom login function that sets the request.session[‘_auth_user_backend’] to be the customer specific backend. Thus, when get_user(user_id) is called on each request, it uses the customer backend which knows which database to request from.

I would like to avoid having to manage an authentication backend for each customer if possible. Is there a better way to do this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T03:53:40+00:00Added an answer on June 12, 2026 at 3:53 am

    Since the auth backend is not calling the QuerySet method using you could use a database router with a thread local variable and some middleware to set the variable to the customer’s database name. The middleware would have to be placed before the authentication middleware.

    The thread local variable is thread safe. It creates a thread local global variable.

    If you were following the path of a request it would do the following:

    1. The request hits django
    2. Your custom middleware grabs the database name from the url sets it to the thread local global variable.
    3. The django authentication middleware starts and sets the user by running the query User.object.get(id=user_id). This will use your database router which will just return the thread local global variable that was set in the previous middleware.

    4. The request continues into the rest of the django stack.

    For example you have the following modules:

    my_app/middleware.py

    from threading import local
    
    my_local_global = local()
    
    class CustomerMiddleware(object):
        def process_request(self, request):
            my_local_global.database_name = get_database_name(request)
    

    my_app/routers.py

    from middleware import my_local_global
    
    class MultiCustomerRouter(object):
        def db_for_read(self, model, **hints):
            return my_local_global.database_name
    

    settings.py

    ...
    MIDDLEWARE_CLASSES = (
     'django.middleware.common.CommonMiddleware',
     'django.contrib.sessions.middleware.SessionMiddleware',
     'django.middleware.csrf.CsrfViewMiddleware',
     'my_app.middleware.CustomerMiddleware',
     'django.contrib.auth.middleware.AuthenticationMiddleware',
     'django.contrib.messages.middleware.MessageMiddleware',
    )
    
    DATABASE_ROUTERS = ['my_app.routers.MultiCustomerRouter']
    ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I`m rewriting an application that use MS Access as the database. I was able
I'm currently rewriting a legacy SharePoint application and I need to figure out how
I have a need to migrate a legacy, desktop based, single user, accounting application
I'm rewriting a data-driven legacy application in Python. One of the primary tables is
We are in the process of rewriting a large legacy system that makes extensive
I am re-writing an app that sits over a legacy database. I'm using NHibernate
I have a legacy PHP application that uses the following URL structure /product_info.php?products_id=YYY where
So, my problem is this. I have a legacy MySQL database that I'm building
Im rewriting application from .NET to PHP. I need to create class like this:
I'm rewriting a legacy access / VBA application in C#. While I am limited

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.