Compatibility with proposed Django 1.5 changes

This patch adds compatibility with the proposed ORM refactoring which
might land in Django 1.5. The patch is backwards compatible with Django
master and older Django versions.