2

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.

  • Unfortunately django settings are immutable at runtime. My next question is the database that holds the DATABASE configs in a django database? – Mitchell Walls Jul 10 '18 at 16:51
  • yes the configs will be held in the apps default database. – Austin Jorgensen Jul 10 '18 at 16:53
  • Possible duplicate of [django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet. file init](https://stackoverflow.com/questions/51081783/django-core-exceptions-appregistrynotready-apps-arent-loaded-yet-file-init) – scharette Jul 10 '18 at 17:45
  • You can look at my answer, it may be of interest, https://stackoverflow.com/a/51082877/7692463 – scharette Jul 10 '18 at 17:46

2 Answers2

1

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()
0

Make one function that loads this variables and make it async, so after you app is ready you load it, but im not sure is this will work properly

https://hackernoon.com/asynchronous-python-45df84b82434

Dirty Sollution is make 1 file for each BD and you call your settings based in what BD gonna work...

Diego Vinícius
  • 2,125
  • 1
  • 12
  • 23