Search code examples
pythondjangoherokudjango-modelsheroku-postgres

Will a class work good without being a model in Django?


I'm currently developing an app which requires simple otp authentication using django

In models.py of an app accounts I've created a class to store the otp without making it as a model as follows,

class temp:
    def __init__(self,otp):
        self.otp = otp
        print(self.otp)

In views.py the code goes as,

g = globals()
... some code    
g["user"+ str(request.POST['username'])] = models.temp(the_otp)

This works completely fine in localhost, Will this work if I deploy this to heroku. If not suggest some other way to store the otp temporarily without making models. Thanks in advance.


Solution

  • No this will not work fine. You might see it working fine in your local development server but there are several problems this approach faces due to the fact that this is using global variables:

    1. Let's assume we need to restart our server for some reason. What happens now? Well your global variables are now lost and your users likely face inconvenience because they now need to regenerate their OTP and feel weird why their OTP is not valid for some reason...
    2. We run multiple processes to serve our users requests efficiently, a request from the user might be given to any of the running processes. What happens here? Well our OTP was generated on one process and the user is now trying to submit it to the other process again facing inconvenience.
    3. Many more similar problems that might occur.

    In general global variables are bad more so in a web server.