Django: First impressions from a non-web developer

January 31, 2009

I write in Java and I don’t do “web stuff”. If you haven’t left already, you might be wondering what a non-web developer is doing looking at Django? I’m hardly their target audience and yet somehow they may have scored a convert.

I got wind of its existence from a talk Leah Culver gave at CUSEC 2009 and realized it could be suitable for a problem I was facing with a project at school. We needed to develop a web frontend, mostly as proof of concept, for a database-backed system we’ve been developing but had been dragging our heels about doing soon. Between the three members of the group, none of us had much experience outside of PHP and we certainly had more important aspects of our system to focus on. The trouble was that without a web frontend, it was going to be difficult to demo our system to our project supervisors. Enter Django.

I’m a self professed “non-web guy”. I don’t know much about “web stuff” (and the use of the term “web stuff” should be telling on its own). To give you some perspective: I hacked a bit with perl and cgi at the start of my undergrad in a fit of boredom at a desk job and the last time I made a website was when frames were in style. So I figured diving into Django should be a good test of how it streamlines the little things for web developers. Thankfully Python is not foreign to me, though my mindset is still partway between Java and C# and thus writing Good Python is still a conscious effort.

After following the tutorial, I had gotten a taste for some of the neat features Django had to offer. I was particularly interested in the object-relational mapping, bullet point #1 on Django’s front page, as this had been a sore point for our project’s development. Paired with the flexible, shiny and virtually automatic admin interface, the solution we sought was staring us in the face. The problem is that we had our model defined in Java and re-doing it in Python, while quite straightforward, was not an option due to time and project commitments.

Slightly dejected, since Django’s admin interface would be sufficient for our demoing needs, I stumbled across Django’s instructions for integrating with existing databases. Jackpot.

First bump: the MySQLdb module doesn’t work with Python >2.5. Alright, no biggie. After that, the

python manage.py inspectdb

command went swimmingly. What followed was series of re-arrangements to the models.py file that was generated in order to get the dependencies in line (the table models are simply output alphabetically). A bit tedious, but a breeze compared to some alternatives.

Running the server with the new model was initially unsuccessful. Validation was failing on a field accessor “clashing” with a related field and instructed me to add a “related_name” for the definition. Looking it up in the docs revealed its intended use for when a table needs to reference itself. This, however, wasn’t the case in my model. We simply had two references from TableA to TableB. A little further searching revealed that the prescribed remedy, giving each field a different related_name, was indeed the correct one. I don’t completely understand how this is not a bug, though perhaps there’s simply a default value for related_name (in which case I understand how the clash occurs and why it’s invalid).

The next problem was… actually there was no next problem. It’s working. I can barely believe it. I’m sure part of it is being so ridiculously out of sync with the web development world that even mundane features would impress me, but I’m impressed. A minor version requirement (for a third-party db adaptor) and some fiddling with a generated file and that was it.

I’m off to play with Django admin options to get wrestle the forms into something a bit more useable, but it’s all gravy from here.

Yes, there are alternatives, some of which might be better for what I’m doing (maybe in every way too). But that’s not the point. Django has lowered the barrier for entry to enable clueless folk like myself to create some cool and useful things. And that’s really the important thing – it’s gotten me to step into the web development realm to see what’s possible. Rails, for all its hype, can’t even claim that (for me).