Global Variable Neded? - Python 2.7.8 / Django 1.7

46 views
Skip to first unread message

Henry Versemann

unread,
May 13, 2015, 5:13:15 PM5/13/15
to django...@googlegroups.com
I have a list of one or more items being returned back to my application as a response from a call to an API. In this particular part of the process depending on the data keys selected by the user, if any some data fields will be formatted automatically. When that happens I need to add the keys to those new automatically-generated fields to the list of data keys originally entered by the user. In the case where there's only one item being returned, this isn't a problem, but when I get back multiple objects in my response I only want to add the new data field keys to the display key list once. The flag will be initialized to false at the beginning of the process which processes all of the responses. So the only way I could think of to do this would be to setup some kind of global variable that can be checked and when set to "false" then and only then update the display key list with the keys to the new data fields. Then also at the same time set the flag to "true", so the update doesn't happen again for each and every subsequent item in the list of response objects. At least not until the next request is sent and its response is being processed.  

So what are my options for creating a global Boolean variable(or any other global variable type for that matter), for accomplishing the above task?

So far I've tried using a "global" keyword when setting up a variable, as well as declaring a variable in a globalvars.py file, importing the file and trying to reference it something like this:

globalvars.display_key_list_updated = True or False

But I keep getting errors of one sort or another with each way I've tried so far (mostly exceptions.NameError).

This is my first attempt at trying to setup and use some kind of a global variable, so its kind of frustrating, and I'm probably making it harder than it is, but my lack of experience is getting in the way. 

Thanks for the help. It is much appreciated.

Henry

Nikolas Stevenson-Molnar

unread,
May 13, 2015, 5:22:08 PM5/13/15
to django...@googlegroups.com
If I understand correctly what you want, then I think sessions will help you here: https://docs.djangoproject.com/en/1.7/topics/http/sessions/

def view_1(self, request):
    request.session['display_key_list_updated'] = True

def view_2(self, request):
    if request.session.get('display_key_list_updated'):
        do_something()

_Nik
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/8a1f173b-de34-4e34-8c34-e1512b46a05d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Gergely Polonkai

unread,
May 13, 2015, 5:29:42 PM5/13/15
to django...@googlegroups.com
Hello,

I think the source of your frustration is that you started on the wrong track. If you create a global variable here, *all* your users will see the same fields. I guess it’s not really the thing you want.

You have several other options here:

1) Add a user setting in the database

If you create a separate model with your User model as its primary key (thus, creating a OneToOne relation between a User and Setting), you can store these values in the database, this way persisting it between user sessions

2) Session variables

You can use Django’s session framework. Here[1] is some help on this.

3) Cache

This can be similar to 1) or 2), or somewhat the combination of the two. This way you store such values in a cache, like MemCached. This solves some possible garbage collection problems and database storage issues. More on accessing the cache here[2].

Hope this helps.

Best,
Gergely


Henry Versemann

unread,
May 13, 2015, 5:29:57 PM5/13/15
to django...@googlegroups.com
Thanks Nik. Your right and I think that will work.
Thanks for the suggestion.

Henry
Reply all
Reply to author
Forward
0 new messages