Antti Kaihola  committed d44f5b1

Documentation: Added a warning and instructions about Johnny outside Django's request-response loop

A common gotcha only mentioned in some closed Bitbucket issues is that
Johnny is not enabled in e.g. scripts, management commands,
asynchronous workers and the shell. I gathered and added to the
documentation some information about this from the following issues in

  • Participants
  • Parent commits 296e5d2

Comments (0)

Files changed (2)

File docs/index.rst

 Johnny does not define any views, urls, or models, so we can skip adding it
+*Note*: Johnny is *not* enabled by default in scripts, management commands,
+asynchronous workers and the shell.  See `the queryset cache documentation
+for instructions on how to enable it in these cases.
 New in this version

File docs/queryset_cache.rst

 .. autofunction:: johnny.cache.invalidate
+Using with scripts, management commands, asynchronous workers and the shell
+Since the QuerySet Cache is enabled via middleware, queries made from outside
+of Django's request-response loop don't use Johnny. For example, saves and
+deletes don't invalidate models in management commands.
+You can enable the QuerySet Cache manually by instantiating the middleware in
+your code before using the ORM::
+    from johnny.middleware import QueryCacheMiddleware
+    qcm = QueryCacheMiddleware()
+    # do some work
+    qcm.unpatch() 
+    # this unpatches and flushes the cache
+To make sure Johnny is always active in management commands, you can enable it
+the project's ```` file::
+    cd /tmp
+ createproject myproject
+Now insert into ``/tmp/myproject/``::
+    from johnny.middleware import QueryCacheMiddleware
+    QueryCacheMiddleware()
+This works because :func:`` always imports
+the project module before executing the management command.