Facing Issues in accessing DB and Instantiating object on app load

I want to load data from db and store it in an object and access that object from anywhere in the Django project.
Preferably using the conventional settings.GlobalSettings where settings are the django.conf.

I have a function that should run while the app starts.

How can I achieve that and is it possible?

I have been trying different implementations and getting errors like

  1. AttributeError: ‘Settings’ object has no attribute ‘GlobalSettings’
  2. django.core.exceptions.AppRegistryNotReady: Apps aren’t loaded yet.

I was trying to call that function as a post_migrate signal in the apps.py.
also, I was calling the settings class using from django.conf import settings and added my GlobalSettings in it and tried to load but nothing worked.

I am fairly new to this, any help would be appreciated.

Why? What is the real objective here? (As opposed to retrieving the data from the db when and where it’s needed, or storing the data in cache.)

Generally not a good idea. What is the specific requirement you’re trying to address?

Correct. Your settings are set and are to be considered “read only”.

If you describe your actual requirements and what the ultimate objectives are, we may be able to suggest appropriate solutions.

Hey, thanks for your response.

As mentioned it is a GlobalSettings object and I need it to be accessed frequently, and other attrs that I use continuously and those attrs depend on the value stored in db.

Any other solution would appreciated.

I was trying to do so; just to reduce repeated object instantiation and repeated db calls.

If you provide a detailed description of the underlying requirement, we might be able to offer some specific suggestions, however …

it is highly unlikely that this is something you should be worried about. If this is the only underlying reason for this, I woudn’t bother.

I am trying to instantiate a Gemini for object and for that, I need values from db; this object is being used in various places. across multiple apps.

Then I suggest you identify a serialized representation of it and store it in a cache.