Package: django-middleware-global-request
django-middleware-global-requestDjango middleware that keep request instance for every thread.Installpipinstalldjango-middleware-global-requestNoteIt's NOT good to use global request, you should pass the request instance from view to anywhere you want to use it.If you use the global request in Model layout, it means when you call the Model method from Django Shell, you get a None request.UsageAdd django applicationdjango_middleware_global_requestto INSTALLED_APPS inpro/settings.py:INSTALLED_APPS=[...'django_middleware_global_request',...]AddGlobalRequestMiddlewareto MIDDLEWARE inpro/settings.py:MIDDLEWARE=[...'django_middleware_global_request.middleware.GlobalRequestMiddleware',...]Useget_requestto get the global request instance from a function inpro/models.py:fromdjango.dbimportmodelsfromdjango.confimportsettingsfromdjango_middleware_global_requestimportget_requestclassBook(models.Model):name=models.CharField(max_length=64)author=models.ForeignKey(settings.AUTH_USER_MODEL,on_delete=models.SET_NULL,null=True,blank=True)def__str__(self):returnself.namedefsave(self,*args,**kwargs):ifnotself.author:request=get_request()ifrequest:self.author=request.userreturnsuper().save(*args,**kwargs)Usewith GlobalRequest(xxx): passto set the global request to a new value in NON-USER-REQUEST context, e.g. inmanagement commandcontext or inpython manage.py shellcontext. Out of thewith scope, the global request instance will be reset to the value when entering into thewith scope.importdjclickasclickfromdjango.contrib.authimportget_user_modelfromdjango_middleware_global_request_example.modelsimportBookfromdjango_middleware_global_requestimportGlobalRequest@click.command()@click.option("-n","--number",type=int,default=10)defcreate(number):admin=get_user_model().objects.all()[0]withGlobalRequest(user=admin):foriinrange(number):book=Book(name="book{idx}".format(idx=i+1))book.save()print(i,book.name,book.author.username)UseGlobalRequestStorageto set the global request instance for current thread context. The global request instance will all time exists until you changed it by another value.example.pyfrom django.contrib.auth import get_user_model
from django_middleware_global_request_example.models import Book
from django_middleware_global_request import GlobalRequestStorage

b1 = Book(name="b1")
b1.save()
print(b1, b1.author)

admin = get_user_model().objects.get(username="admin")
GlobalRequestStorage().set_user(admin)

b2 = Book(name="b2")
b2.save()
print(b2, b2.author)

b3 = Book(name="b3")
b3.save()
print(b3, b3.author)example result in python3 manage.py shell contexttest@test django-middleware-global-request % python3.9 manage.py shell
Python 3.9.13 (main, Jun  8 2022, 15:40:49) 
Type 'copyright', 'credits' or 'license' for more information
IPython 8.3.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]: import example
b1 None
b2 admin
b3 adminTest Passedpython27:~=django1.11.29python34:~=django1.11.29python34:~=django2.0.13python35:~=django1.11.29python35:~=django2.0.13python35:~=django2.1.15python35:~=django2.2.28python36:~=django2.0.13python36:~=django2.1.15python36:~=django2.2.28python36:~=django3.0.14python36:~=django3.1.14python36:~=django3.2.21python37:~=django2.0.13python37:~=django2.1.15python37:~=django2.2.28python37:~=django3.0.14python37:~=django3.1.14python37:~=django3.2.21python38:~=django2.0.13python38:~=django2.1.15python38:~=django2.2.28python38:~=django3.0.14python38:~=django3.1.14python38:~=django3.2.21python38:~=django4.0.10python38:~=django4.1.11python38:~=django4.2.5python39:~=django2.0.13python39:~=django2.1.15python39:~=django2.2.28python39:~=django3.0.14python39:~=django3.1.14python39:~=django3.2.21python39:~=django4.0.10python39:~=django4.1.11python39:~=django4.2.5python310:~=django2.1.15python310:~=django2.2.28python310:~=django3.0.14python310:~=django3.1.14python310:~=django3.2.21python310:~=django4.0.10python310:~=django4.1.11python310:~=django4.2.5python311:~=django2.2.28python311:~=django3.0.14python311:~=django3.1.14python311:~=django3.2.21python311:~=django4.0.10python311:~=django4.1.11python311:~=django4.2.5Releasesv0.1.0First release.v0.1.1Some changes.v0.1.2Some changes.v0.2.0Rename the core package from django_global_request to django_middleware_global_request so that it matches with the package name.Note:It's NOT backward compatible, all applications that using old name MUST do changes.v0.3.0AddGlobalRequestandGlobalRequestStorageto set the global request instance value for NON-USER-REQUEST context.v0.3.1Addapp_middleware_requiresto module __init__.py to work withdjango-app-requires.v0.3.2Doc update.v0.3.3All unit test passed.