[Answered ]-How to pass config setting in Django and keep module decoupled?

1👍

You could have a my_settings.py holding the PATH:

# my_settings.py
import os
RELEVANT_PATH = os.environ.get("whatever", "/some/default")

and use like

# datastore.py
from my_settings import RELEVANT_PATH

class SomeDataStore:
    def list_files(self):
        p = Path(RELEVANT_PATH) 
        files = p.glob("*.csv")
        return files
    ...

In case you need this path whithin Django elsewhere, you could have this path as part of settings.py as well

# settings.py
from my_settings import RELEVANT_PATH as my_relevant_path

RELEVANT_PATH = my_relevant_path

# usage in other django app files
from django.conf import settings
# use settings.RELEVANT_PATH as usual

This would provide for some decoupling and you can change the path at a single place my_settings.py and import the path outside django as well as use it inside django with the usual django.conf.settings.xx syntax.

👤Nechoj

Leave a comment