[Fixed]-Global variable in django (singleton)

1👍

You can add a function to the User object to get the Auth class instance, you can also use propery caching to only create it once:

from django.contrib.auth.models import User

def user_get_auth(self):
    # Initialze the Auth class here

    return Auth()


User.add_to_class("get_auth", property(user_get_auth))

in the views.py or templates you can get the Auth class by:

request.user.get_auth.some_method()
👤ahmed

Leave a comment