Suppose I have two apps: data
and visual
.
App data
executes a database retrieval upon starting up. This thread here and this doc advises on how and where to place code that executes once on starting up. So, in app data
:
#apps.py
from django.apps import AppConfig
global_df #want to declare a global variable to be shared across all apps here.
class DataConfig(AppConfig):
# ...
def ready(self):
from .models import MyModel
...
df = retrieve_db() #retrieve model instances from database
...
return df
In the code above, I am looking to execute ready()
once on starting up and return df
to a shared global variable (in this case, global_df
). App visual
should be able to access (through import
, maybe) this global_df
. But, any further modification of this global_df
should be done only in app data
.
This thread here advised to place any global variable in the app's __init__.py
file. But it mentioned that this only works for environmental variable
.
Two questions:
1 - How and where do I declare such a global variable?
2 - On starting up, how to pass the output of a function that executes only once to this global variable?
Some threads discuss Redis for caching purpose. But I am not looking for that solution as it seems like an overkill for the problem I am having.