It's on!
Leo writes about his experience porting Django to Jython and there's a thread on the django-developers group too.
Funny how things seem to click for a bunch of people all at once - the release of Jython 2.2 seemingly the catalyst.
For my part I've been working on porting sections of Django to Jython on and off for a couple of weeks. Whilst Java is great for paying the bills, developing web applications with it is pretty slow (by comparison).
My stack from bottom up looks something like this MSSQL -> {Spring: [Hibernate , Service tier]} -> Web framework. Yes, it's Enterprisey in the pejorative sense! ;)
One of the web frameworks is actually Flex Data Services, which is great for RIAs but terrible where you want to bang together a quick admin app.
I figured the architectural gaps between Velocity templates and the enterprise business tier I mentioned above could be filled very nicely with parts of Django, such as New Forms: Where Leo adopts a more holistic approach outside of the container, I've been fiddling around with the appealing concept of Django MVC as Struts 3.0!!
Lessons
Today, Django in Jython not only involves not only resolving the differences between CPython and Jython, but also back-porting to Python 2.2. It's easier said than done, but here are a couple of random things I've found.
For django.utils.datastructures the yield keyword is required which in 2.2 is imported from future:
from __future__ import generators
An annoyance is the absence of shallow or deep copying for class instances. In modern CPython, the following is possible:
>>> import copy
>>> class A(object):
... var = 10
...
>>> a = A()
>>> b = copy.copy(a)
>>> a.var = 15
>>> a.var
15
>>> b.var
10
But in Jython:
>>> import copy
>>> class A(object):
... var = 10
...
>>> a = A()
>>> b = copy.copy(a)
Traceback (innermost last):
File "<console>", line 1, in ?
File "/Users/cam/Java/jython/Lib/copy.py", line 78, in copy
Error: un(shallow)copyable object of type <class '__main__.A'>
This becomes important as django.newforms.forms.BaseForm takes a copy of base_fields for the instance to manipulate. The solution is to implement __copy__ on django.newforms.fields.Field and components, something I've part completed.
According to the Jython doc:
The dictionaries used by classes, instances, and modules in Jython are not the same as the dictionaries created by {}.
Instead they are instances of org.python.core.PyStringMap, which doesn't implement the same methods as dict, most notably {}.pop(x).
I got the desired behaviour, (albeit clumsily) like this:
def __new__(cls, name, bases, attrs):
fields = [(field_name, attrs.get(field_name))
for field_name, obj in attrs.items() if isinstance(obj, Field)]
...
for n,o in fields:
attrs.__delitem__(n)
...
As you've probably guessed, there is quite a bit to do here. Having read Leo's post, I'm not sure whether it's best to pursue Django within the web container, outside of it, or both.
Deploying to the container would broaden the appeal of Django in the enterprise, but I don't know it can be achieved without forking the source. Perhaps the effort would be better spent contributing to the 1.0 release? Certainly Jython will have come along a little further by then too.
Thoughts?

Feed
Comments 10
Excellent to see another person interested in seeing a working Django on Jython. I'm not really going anywhere with it myself at the moment, partially I'm torn between trying to backport Django to Jython 2.2 (your option, and my original one) and using Jython SVN, which is closer to Python 2.3 (Leo's option, and I dabbled there too). Jython SVN doesn't have the
yieldproblem, but it does still have the PyStringMap issue.Ideally, Jython's PyStringMap would support
pop(), and the Pylons folks have considered just that, but otherwise, it's a tough call on how best to do it. Leo creates a new dictionary and calls itsupdate()with the PyStringMap. I've also found that it can be as simple asattrs = dict(**attrs)and the rest works fine. The Pylons folks seem toBut there are many such cases, which penalize CPython users (in terms of performances) in order to support Jython. It remains to be seen whether they'll add up enough to require a separate fork in order to be worthwhile.
Posted September 6, 2007 at 12:40 a.m. ¶Hi Marty,
Thanks for all that great info. What was your experience with Jython SVN? Stable?
I figure if the aim is to get Django into the enterprise it will need to run on a Jython release, or at least a release candidate.
c.
Posted September 6, 2007 at 9:17 a.m. ¶Well, I've been having problems with it myself, but I fully expect I'm building it wrong somehow. I expect Leo wouldn't be publishing his findings unless it was a reasonably stable solution.
Posted September 7, 2007 at 3:28 a.m. ¶If you decide to look at Jython's trunk, I just added pop() to PyStringMap today.
Posted September 7, 2007 at 3:30 a.m. ¶Also, I'm looking to set up an aggregate of the various Django on Jython blogs that are running so we can track progress more easily. I notice you have a Django-specific feed set up, but could you set one up for your Jython tag as well?
Posted September 7, 2007 at 3:38 a.m. ¶@Frank - Wow! That's very cool. I think I'll check it out ;)
@Marty - Done. Jython feed
Posted September 7, 2007 at 9:19 a.m. ¶If you're looking for something to fill the "gaps between Velocity templates and the enterprise business tier", maybe CherryPy might be a better fit than a full-stack framework like Django. I'm currenty playing with Jython, CherryPy and Velocity with some success at the moment. For info about running in a container, this might be of interest: http://henkenotes.blogspot.com/2006/1...
Posted September 7, 2007 at 6:45 p.m. ¶Thanks Henrik. You might be on to something there. I was mostly thinking of Django because I love the framework, and am reasonably familiar with the source. Subscribed to the feed of that blog - ta!
Posted September 8, 2007 at 11 p.m. ¶Hey, good to see more people with common goals!
About the deploy thing, I'm all for getting Django running on Servlet Containers. Modjy should make this easy, keeping us for forking anything.
Are you looking forward into publishing your changes in some way?
Posted September 10, 2007 at 5:29 a.m. ¶@Leo - I haven't got a whole heap to show for my efforts at this point because I was more working towards using parts of Django to glue the enterprisey Java stack together. That aside, I'd love to see Django under Jython. Do you think an official branch is justified. I'd be keen to contribute.
c.
Posted September 10, 2007 at 11:08 p.m. ¶Comments are now closed.