[storm] Using Storm with Django

James Henstridge james at jamesh.id.au
Thu Jul 31 11:03:04 BST 2008


Hi everyone,

As hinted at earlier, I've been working on integrating Storm with
Django.  I've put a branch up on Launchpad, which can be downloaded
with:

    bzr branch lp:~jamesh/storm/django-support storm

The branch is a work in progress, so the APIs may change.  Here is a
quick guide to making use of the code:

1. install the "zope.interface" and "transaction" modules (either from
pypi.python.org or Zope subversion should do).

2. In the settings file for your application, add
storm.django.middleware.ZopeTransactionMiddleware to
MIDDLEWARE_CLASSES.  This configures the Zope transaction manager to
run a transaction over each Django request.

3. Define STORM_STORES in the settings file to be a dictionary mapping
store names to database URIs

4. In your view code, you can get a store with:
    from storm.django.stores import get_store
    store = get_store("name")

Then use the store as you would normally.  This uses the existing
zstorm code, so you get long lived per-thread connections and
integration with the Zope global transaction manager (and the
middleware causes the commits to occur at the right places).


One thing to note is that the ZopeTransactionMiddleware class will not
commit on all requests.  If the request is a GET or HEAD, then the
transaction will be aborted instead (since the request should be
idempotent).  This policy can be turned off by setting
STORM_COMMIT_SAFE_METHODS to True.

The changes to be able to use the storm.zope.zstorm code with a cut
down Zope are available in my lp:~jamesh/storm/require-less-zope
branch, which is essentially the same as the patch I posted earlier
with a change to run most of the zstorm tests when in this
configuration.  This section of the changes is already up for review,
even though the remainder is still in progress.


One thing this branch does not help you do is to use some of the
existing Django infrastructure with Storm classes.  At a minimum I'd
like to get the Django admin interface working with Storm classes.

James.



More information about the storm mailing list