简体   繁体   中英

How to make the 'return' from a django model conditional to the value of a global variable?

I am a novice in both Python and Django, and I am struggling to find the way to make a 'return' from a Django model conditional upon the value of a global variable.

More specifically, I am designing an app to run an interview with the user / visitor. In the interview the questions to the user can be in past or present tense. This depends on an introduction view from which I get the choice from the user, ie either 'past' or 'now'.

My model is

class Question(models.Model):
    key = models.CharField(max_length=50)
    value = models.CharField(max_length=50)
    question_text_past = models.CharField(max_length=2000)
    question_text_now = models.CharField(max_length=2000)
    def __str__(self):
        global TENSE
        if TENSE == 'past':
            return self.question_text_past
        else:
            return self.question_text_now

However, no matter what I've tried (while testing the code within the shell), my model does not reflect the changes in the value of TENSE. The model returns a "not defined" error. I've also tried to put TENSE into a module globals.py and import this into my models.py with no success. I might be missing something simple but fundamental about Django, so I appreciate any help.

You can't implement this logic using a global variable in the modules file.

I suggest to call .question_text_past or .question_text_past on the Question object at the location where the logic happens.

I've found three different possibilities to eliminate / replace global variables in my app. In my app (ie running a virtual interview with a user) I use the same template over the same view, but the variables within the view change their values depending on the user's choice / response. It is a little bit like a function calling itself with new arguments, but having to "remember" what were the old ones.

First. I set up a classes.py module which contains the class Global. Each attribute replaces a previously global variable. Then import this class in views.py. This avoids Django handling the class as a DB table.

Second. Create the same class as a model. List and dictionary types are defined as JSONField. Here an initial view creates a new instance, a DB record with an id. This id, however, may still need to be "globally" accessible throughout the interview view. One solution was to start a Django Session and use session_key (an awful string) as the id of the newly created record. The other choice was to use a "global" variable through Constance to assign the automatic id to it in settings.py.

Third. Use Constance for all global variables. However, after reading the docs, I had the impression that Constance was not designed to accommodate frequently changing dynamic variables, and it may be problematic using it to store complex data types, eg lists, dictionaries. But, again, this was just an impression.

I would appreciate your opinion / guidance / suggestion.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM