Dealing with global or session objects

11 views
Skip to first unread message

Jean-Noël Colin

unread,
May 22, 2019, 9:26:15 AM5/22/19
to Django users
Hi, 

I'm coming from a java world, where there exists different scopes for objects: application-, session-, request scopes all define different lifecycle for objects. I have some difficulties in finding an equivalent approach in Django. 

Here's the need: my project stores its custom configuration in the database, for persistence; these parameters can be changed by an administrator only, through a custom view/form. However, those parameters are used in many views in the project, in read-only mode. So my idea would be to have one single object, that stores the parameter values, that is initialized when the application starts, that can be read at any time without requiring reading from the DB, and that, when modified by admin, would be saved to the db. How to plug this in the django architecture? 

Thanks a lot

Jean-Noël

Simon A

unread,
May 23, 2019, 6:57:21 AM5/23/19
to Django users
Hello fellow Java user,

If I would do it, I'd setup a model to store those configurations.

Then on the app, upon login or upon loading the initial page of the app, I will perform the operation to retrieve the details from the database, and then dump to the django sessions object.

# get operation
request
.session.get('key', None)


# set operation
self.request.session['key'] = value

Please also see https://docs.djangoproject.com/en/2.2/topics/http/sessions/ for more information.

Regards,
Simon.
Reply all
Reply to author
Forward
0 new messages