Sunday, September 2, 2007

Django on Jython: What I've done until now



Now that Jython has released it's 2.2 version and has some progress on 2.3 features, I decided to try Django on it. As you can see on thte screenshot above, I got the development server running. And there is some progress on getting database syncing to work:

lsoto@colinux:~/src/hellodjango$ createdb hellodjango
CREATE DATABASE
lsoto@colinux:~/src/hellodjango$ CLASSPATH=/usr/share/java/postgresql.jar ~/src/jython/trunk/jython/jython.sh manage.py syncdb
Creating table auth_permission
Creating table auth_user
Creating table auth_message
Creating table auth_group
Creating table django_content_type
Creating table django_session
Creating table django_site

You just installed Django's auth system, which means you don't have any superusers defined.
Would you like to create one now? (yes/no): yes
Username: admin
E-mail address: admin
Error: That e-mail address is invalid.
E-mail address: admin@admin.com
Warning: Problem with getpass. Passwords may be echoed.
Password: 123
Warning: Problem with getpass. Passwords may be echoed.
Password (again): 123
Traceback (innermost last):
File "manage.py", line 11, in ?
[...]
File "/home/lsoto/src/django.svn.trunk/django/db/backends/postgresql_zxjdbc/base.py", line 86, in execute
Error: ERROR: la columna «is_staff» es de tipo boolean pero la expresión es de tipo integer [SQLCode: 0], [SQLState: 42804]

So here are the steps I've done until now:

  1. Get the development (SVN) source of both project. As Django requires python >= 2.3, I didn't tried Jython 2.2. I'm using mercurial to maintain my "branch", so I made a mercurial mirror of django svn repository and another mercurial repository for my changes

  2. Build Jython using ant

  3. Add Django directory to Jython path (copying the registry file to dist/ and editing the python.path line).
  4. Add some modules from Jython's CPythonLib/ to dist/Lib directory: optparse.py, gettext.py, locale.py and getpass.py

  5. Cook a simple script to run jython from the shell

  6. Apply these two patches to jython. The later is not really a fix, but it allowed me to forget that bug for a while.

  7. Modify django source where needed:

    1. Replace __builtins__ with __builtin__ (changeset)

    2. Make a new backend: postgresql_zxjdbc

    3. Avoid using colors on the tty if running on java (changeset).

    4. Don't assume that iterables have an __iter__ attribute (two changesets)

    5. Workaround Jython problem when trying to insert non-string keys on some dicts (changesets here, here and here)

    6. Make a special case for on robustapply for Jython's reflected functions (changeset)

    7. Support non-descriptor methods on django.dispatch.saferef.BoundMethodWeakRef (changeset)

    8. Workaround Jython's isclass() bug (changeset)

    9. Support zxJDBC's autocommit attribute (on other backends it's a method) (changeset)

    10. Avoid filesystem permissions checks if running on jython
    11. (changeset)

All the changes concerning Django are on my mercurial repository, so you can easily get a patched source tree using mercurial:

$ hg clone https://hg.leosoto.com/django.jythonport

Pending short-term goals:

  • syncdb should work

  • The test suite should run

11 comments:

H Eriksson said...

Excellent work. I did some work a few months ago trying to get CherryPy running on Jython (as a first step to get Turbogreas support). This was prior to A Kennedy's new Socket stuff, so I didn't get it to work back then and haven't had time to try it again. This has inspired me to give it another shot though. Keep up the good work :-)

Philip Jenvey said...

I know Frank Wierzbicki also attempted this, good to see more folks looking at Jython. I'm in the process of getting setuptools/distutils working, then afterwards the Pylons stack

Leo Soto M. said...

H Eriksson: Yeah, give it another chance. The new socket module hasn't given me any problem on my Django work.

Philip: I can't wait for your work on Pylons, and specially SQLAlchemy. Getting such great ORM working on Jython would be extremely cool.

Adam Hupp said...

Regarding SQLAlchemy on Jython, Frank Wierzbicki has some changes to make that work. He showed it at the trizpug meeting a little while ago.

Leo Soto M. said...

Hey, how I missed that?

Philip Jenvey said...

SQLAlchemy is also thinking of Jython in the back of their minds. Their Dialect Refactor take II Ticket will address configuring SQLAlchemy dialects with Jython's zxJDBC (the Python DBAPI jdbc tie in)

H Eriksson said...

I tried out SQLAlchemy with Jython and SQLite yesterday and managed to do sort of what Frank did earlier. When I started mapping classes and using the real ORM I ran into trouble though... The dialect refactoring looks promising.

H Eriksson said...

Just wanted to add that I finally got CherryPy 3 working with Jython :-)
See my blog

Leo Soto M. said...

H Eriksson: How do you get SQLite working with Jython?

H Eriksson said...

I used zxJDBC and an SQLite jdbc driver (http://www.zentus.com/sqlitejdbc/).

VK said...

Leo,

Good work! Since the benefit of Jython is that it makes possible calls to Java classes, would'nt it be easier if we let django stick with Python and install jython on the same system and let jython do all the interafacing with Java classes you would like to make use of.

Then Jython can return the Java class output to Django for web display and further Django magic?

I'd be interested in your thoughts in this.