Create globally available objects based on a model data

I have a model called CustomUser, and I’d like for each entry to initialise a separate object that is globally available (across the whole project).

Intuitively, I’d make sense to put this in settings.py but the models aren’t yet initialised. What is the best way to do this?

Thanks!

Models are globally available. What specifically are you trying to achieve?

Thanks, so I have a model called CustomUser (which is completely different than the default Django User model). And for each entry of CustomUser I would like to initialise a custom HTTP client, that interacts with an external API.

I need each of these clients to be available across all my project.

The reason for this is that CustomUser Django model, contains authentication details needed to create my HTTP client. In addition these clients will be used throughout my Django project, hence why I need to access them globally.

Thanks!

This is starting to sound to me like it might be an X-Y Problem.

Please describe in more detail how you envision this client is going to be used, the sequence of events surrounding its usage, and what you think the “lifespan” of this client is going to be.

Keep in mind that code you write does not execute outside the context of a request-response cycle. There is no way within core Django to create a persistent process that is going to continue to execute after a view returns a response. If you’re thinking of doing anything like this, you’re going to need to set this up as an external task, using something like Celery to manage those tasks.

I see, let me try to explain in more detail.

Amongst other things my CustomUser user model contains a KEY and SECRET.

Inside my Django project I have a store-manager client which interacts with a third-party store via HTTP API.

In a separate process/thread managed by django-apscheduler, I run a program that executes multiple actions with third-party store on behalf of each user in the CustomUser table.

So at the beginning of process spawned by django-apscheduler, I’d like to create store-manager client object (one per CustomUser) using their respective KEY and SECRET. I need these to be persistent throughout the session.

I tried to add a Manager function to the CustomUser model called 'getAllStoreClients()` but that doesn’t seem to have the intended behaviour - Clients do not persist during the session.

What is this “store-manager client” supposed to do?

Why can’t your external process read the CustomUser object to retrieve the credentials as necessary?

Why can’t the external process store data as necessary in a model?

What session are you referring to here? What do you mean by “persistent” in this context?