Search code examples
djangopython-3.xdjango-settings

Dynamically set Django settings variables from Database


I am currently trying to build an application that manages multiple databases. Since the app will be managing data in 30+ databases I am attempting to generate DATABASE_ROUTERS in the settings file. I cannot directly import the db model into the settings file. I get this error:

django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.

This error makes since. Is there a way I can control the sequence of events so that I have access to the database before all of the settings are established on execution? My goal is to automate database connections pulling relevant data from a DB and generate the DATABASE_ROUTERS and DATABASES within the setting file. Is this even possible? Is there a package that I can download that does exactly this?

If you do not know what I am asking please do not down vote just ask me to elaborate.


Solution

  • I was able to figure out how to query the data I needed from my database and import it into the settings file. I created the script below. Keep in mind this can be improved, this is just something I modified from here. This directly queries data from my test db (sqlite3). I use postgreSQL in production. This script should work, with some modification, with PostgreSQL.

    As you can see below I am storing the data in dictionaries that is then stored in a list. I then import that list of dictionaries into my settings file. From there I can loop through the list and create my DATABASE_ROUTERS and DATABASES dynamically from the database. I was also able to generate router Classes in my routers.py file by importing the same list. Please comment below if you need me to elaborate further.

    import sqlite3
    from sqlite3 import Error
    dbs = []
    
    def create_connection(db_file):
        """ create a database connection to the SQLite database
            specified by the db_file
        :param db_file: database file
        :return: Connection object or None
        """
        try:
            conn = sqlite3.connect(db_file)
            return conn
        except Error as e:
            print(e)
    
        return None
    
    
    def select_all_data(conn):
        """
        Query all rows in the table
        :param conn: the Connection object
        :return:
        """
        cur = conn.cursor()
        cur.execute("SELECT * FROM fund_table")
    
        rows = cur.fetchall()
    
        for row in rows:
            print(row)
    
    
    def select_name_and_db(conn):
        """
        Query table by fund_name and db_name
        :param conn: the Connection object
        :return:
        """
        cur = conn.cursor()
        cur.execute("SELECT fund_name, db_name FROM fund_table")
    
        rows = cur.fetchall()
    
        for row in rows:
            dbs.append({"fund_name": row[0], "db_name": row[1]})
        return dbs
    
    
    def main():
        database = "edb.sqlite3"
    
        # create a database connection
        conn = create_connection(database)
        with conn:
    
            """ select_all_data(conn) """
            select_name_and_db(conn)
    
    main()