2

I have two databases (default database and sample database) . I want to use auth_user table from sample database not from default database (by default , it is taking from default database ).i want specify it in my model file so that i can access in my views . how is this possible?

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'test',
        'USER': 'root',
        'PASSWORD': 'test@123',
        'HOST': '10.40.40.170',
        'PORT': '3306',
    },
    'sample': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'test1',
        'USER': 'root',
        'PASSWORD': 'test@123',
        'HOST': '10.40.40.170',
        'PORT': '3306',
    },
}

I have added one extra column for auth_user table from sample database. I want to use that table. (I can't use auth_user from default database, because my requirement is like that)

Reference. suppose i have 2 models (test1, test2).test1 is from sample1 database and test2 is from sample2 database.is it possible to mention in model file that test1 should come from sample1 database??

in model.py 
class tes1():
    class meta:
         app_label='users'
         db_table='auth_user'
         db_label='sample1' #database name here
Vani Polnedi
  • 595
  • 2
  • 4
  • 19
  • 1
    The way you asked the question is not correct. Please make it more clear. What do you mean by it is taking from x database (are you using any program to access it ). You want this change in your program or in database itself... etc. Please make it more clear – smilyface Dec 26 '18 at 07:07
  • edited question – Vani Polnedi Dec 26 '18 at 10:18

2 Answers2

2

You will have to create Router for that.

Please go through following links

smilyface
  • 5,021
  • 8
  • 41
  • 57
1

You can try this Django Authentication on different databases and Django Doc

shafik
  • 6,098
  • 5
  • 32
  • 50