[Django]-Where in Django can I run startup to load data?

7👍

@Ken4scholar ‘s answer is a good response in terms of "good practice for loading data AFTER Django starts". It doesn’t really address storing/accessing it.

If your data doesn’t expire/change unless you reload the application, then using cache as suggested in comments is redundant, and depending on the cache storage you use you might end up still pulling from some db, creating overhead.

You can simply store it in-memory, like you are already doing. Adding an extra class to the mix won’t really add anything, a simple global variable could do.

Consider something like this:

# my_app/heavy_model.py
data = None

def load_model():
    global data
    data = load_your_expensive_model()

def get_data():
  if data is None:
      raise Exception("Expensive model not loaded")
  return data

and in your config (you can lean more about applications and config here):

# my_app/app_config.py
from django.apps import AppConfig

from my_app.heavy_model import load_model

class MyAppConfig(AppConfig):
    # ...
    def ready(self):
        load_model()

then you can simply call my_app.heavy_model.get_data directly in your views.

⚠️ You can also access the global data variable directly, but a wrapper around it may be nicer to have (also in case you want to create more abstractions around it later).

3👍

You can run a startup script when apps are initialized. To do this, call the script in the ready method of your Config class in apps.py like this:

class MyAppConfig(AppConfig):
    name = 'myapp'

    def ready(self):
        <import and run script>

Leave a comment