I have one database that I specifically using for Django. I ran into a simple problem. For the user authentication in the login I need to use data from another database(mysql) and table. Both the tables are in the same server. How can I do that? Thanks
Share
You need to define both the databases in your
settings.py. You should read through setting up multiple databases documentation. It’s pretty straight forward. Once you have databases configured and synchronized, you can manually select a database for aQuerySetas follows:Update:
To specify a connection for a model you can define a
Routeras follows.and then for your models in models.py you can do something like this:
Update#2
you can setup the router anywhere you like. All you need to do is pass the Routers path to the
DATABASE_ROUTERSvariable in thesettings.pyfile. For the sake of cleanliness of code and being organised i generally do this in a separaterouters.pyfile under my application folder and then set theDATABASE_ROUTERSexample: