The most important thing to know about Django on Jython is that we are almost there, and with clean code. End-to-end functionality is demonstrated by the admin tool running in full CRUD, along with a substantial number of unit tests and syncdb. But this has been achieved by so far requiring only 6 lines of code in changes to Django trunk. (There will be more, however, see below.)
Running on Jython
To run Django on Jython, with a PostgreSQL backend, the following steps are necessary:
- Use the Modern branch of Jython. This consolidated the bugs, workarounds, and patches of numerous people -- plus a bunch more -- in a stable, almost-ready-to-be-merged-into-trunk version of Jython. The most important aspect is that we have tried to make Jython conform more to CPython, using Django as our guide, although there are some gaps -- especially if Django already had incorporated fixes. Our driving goal is to converge on these gaps over time. Please note that is intended to be stable, performant code.
- Use the Django trunk (tested with rev 6992, later should be OK too).
- Apply these two patches, django.dispatch.robustapply (diff) and django.views.debug (diff) due to Leo Soto. I would imagine these will be in Django trunk soon.
- Copy these three files from CPythonLib to Lib: gettext.py, locale.py, optparse.py. Please note that these files are only partially working on Jython, that's why they haven't been promoted yet (gettext.py actually works, as verified by test_gettext.py, but depends on still failing locale.py). But they are very close, and they appear to be fine for Django. Certainly fine for this round of development!
- Use the database backend zxjdbc_postgresql, which was contributed by Leo Soto. Frank Wierzbicki has an experimental backend for MySQL, this should be incorporated soon.
Here's what works:
syncdb and the very cool Django admin run; many unit tests pass. You can run with internationalization enabled. You do need to run the dev server with --noreload for now. We need to document here how to run with modjy, which is Alan Kennedy's servlet container for WSGI apps.
In running the model unit tests, here are the things we seem to be missing, accounting for most of the approximately 75 failures:
- Many doctests are fragile, because they depend on the dict traversal ordering; in Jython, this is different that CPython, and if we adopt ConcurrentHashMap, it's not even repeatable. This would seem to be a pervasive bug in Django.
- We still have some encoding problems, again seen in doctests. An example where output is expected to be lower case hex, not upper case. I fixed the problem in PyUnicode, but there are more places.
- Problem with the ManagerDescriptor handling, in django.db.models.manager.
- No decorators yet! (But they are coming soon, and are now available experimentally for Jython in the newcompiler work I have been leading.)
There may be some other rough categories, we need to look at the failures more systematically. All that doctest noise is certainly annoying!
On the Django front, get more of the unit tests running!
Before we can push modern into trunk, the following needs to be done:
- The test_extcall unit test currently fails. This appears to be a dependency on dict traversal being repeatable, a bad assumption. However, it's a mind bending test. The 2.3 version is particularly problematic because it's not modular at all. Google's GHOP has just produced an improved version for Python 2.6 - we will look at this as a starting point.
- Tristan King provided a near complete subset of the functionality for time.strptime, as implemented in org.python.modules.time.Time. This needs to be enhanced. I just tested this, and all unit tests in the CPythonLib version of test_time now pass except for strptime -- specifically the conversion specifier '%c' -- so we can also move to that, and discard our Jython version, when this is completed. That should be soon!
- Decide whether we should use ConcurentHashMap or not as the backing hash map for dict and __dict__. CHM introduces creation overhead, but it should prove to be far more scalable on multicore systems. The programming model is also far nicer with respect to Jython.
Updates - 2008-06-24: I should have put this up a while ago, but Django on Jython is becoming a reality. Most importantly, Leo Soto is working with me through the Google Summer of Code on this project. The modern branch was merged into a trunk earlier this year, and has since been retired. CHM in fact has the right semantics, something I may discuss at a future point. Django has redone the doctest dict literals that were causing problems, and Leo provided a general solution when used with XML/XHTML.
Updates - 2010-09-13: Django on Jython is a standard PyPI project. The best reference for it can be found in our Jython book. Jython 2.5.2 is about out. The previous release fixed pretty much all of the other issues. And using ConcurrentHashMap has been a very good decision!