Caktus Blog
2009

Creating recursive, symmetrical many-to-many relationships in Django
In Django, a recursive many-to-many relationship is a ManyToManyField
that points to the same model in which it's defined ('self')
. A
symmetrical relationship is one in where, when a.contacts = [b]
, a is
in b.contacts
.

Setting PostgreSQL's SHMMAX in Mac OS X 10.5 (Leopard)
If you've ever tried to increase the shared_buffers setting in your postgresql.conf to a value that exceeds the amount of shared memory supported by your operating system kernel, then you'll see an error message like this:

Towards a Standard for Django Session Messages
Django needs a standard way in which session-specific messages can be created and retrieved for display to the user. For years we’ve been surviving using user.message_set
to store messages that are really specific to the current session, not the user, or using the latest and greatest Django snippet, pluggable app, or custom crafted middleware to handle messages in a more appropriate way.

Remote logging with Python logging and Django
As part of my work on EveryWatt, our fledgling energy monitoring web site, I needed a way to consolidate log messages from all the data loggers we have running in a single place. If you're not familiar with it, Python's logging module is good stuff and worth checking out. We already used it for logging to files locally, and the module defines an HTTPHandler that can deliver log messages to a remote server via HTTP.

Testing Django Views for Concurrency Issues (Updated for Django 4.2 in 2024)
Editor's note: This post was originally published in May, 2009 and was updated in December, 2024 to incorporate changes in Django and improvements suggested by our readers. It has also been tested for compatibility as of the Django 4.2 release.

Explicit Table Locking with PostgreSQL and Django
By default, Django doesn't do explicit table locking. This is OK for most read-heavy scenarios, but sometimes you need guaranteed, exclusive access to the data. Caktus uses PostgreSQL in most of our production environments, so we can use the various lock modes it provides to control concurrent access to the data. Once we obtain a lock in PostgreSQL, it is held for the remainder of the current transaction. Django provides transaction management, so all we need to do is execute a SQL LOCK statement within a transaction, and Django and PostgreSQL will handle the rest.

Parsing Microseconds in a Django Form
There's currently no way to accept microsecond-precision input through a Django form's DateTimeField. This is an acknowledged bug, but the official solution might not come very soon, because the real fix is non-trivial.

Seamlessly switch off (and on) a Django (or other WSGI) site for upgrades
In preparation for migrating the EveryWatt database from one machine to another, I wrote this little WSGI script to easily disable the site while I copy the data. Since it doesn't depend on Django or really anything else (other than a functioning WSGI server), you can use it for other upgrades, too.

Eclipse Ganymede and Subclipse on Ubuntu - JavaHL (JNI) not available
I finally got around to updating my Eclipse, PyDev, and Subclipse environment today, which I use for Django development.
Formerly I was using the SvnKit (pure-Java) libraries. SvnKit "felt" slow to me, compared to my command line SVN client, so this time I tried to get the JavaHL (JNI) libraries working.
