0

What is the scope of Django's Middleware objects? I wish to know if the Middleware objects are initialized for every request.

Relevance: While trying to use SQLAlchemy ORM along with Django, one needs to point a callable object as the scope marker to use the SQLAlchemy's scoped_session. And I'm trying to set the SQLAlchemy session in request scope.

Kalaivanan
  • 111
  • 3
  • [The examples in the docs](https://docs.djangoproject.com/en/1.11/topics/http/middleware/#writing-your-own-middleware) show what is initialised once and what is called for each request/response. – Alasdair May 24 '17 at 13:49
  • @Alasdair, Thanks. I've been going through the docs umpteen times, but missed to notice the comments. – Kalaivanan May 25 '17 at 11:04

1 Answers1

-1

Yes! The middleware is triggered for every single request.
Please go through the documentation to learn more about middlewares and how to write one and use it properly.

For your requirements you should be able to do what you want in process_request (Django < 1.10) or at the start of __call__ (Django 1.10+)

Resley Rodrigues
  • 2,218
  • 17
  • 17