Moving the blog to Django

The long-awaited (by me) conversion of this blog to Django is underway. After a couple hours' work I have a full set of models and a functioning admin, and working index and detail views of postings and comments. Searching, posting comments, and tags are the major pieces remaining. Because of my busy schedule I’ll only be able to work on it in fits and starts, but I expect the total labor in the end to be about five hours.

I’m curious what the line count of the final product will be. The PHP version, excluding my general-purpose admin class and third-party stuff like Markdown, is about 1000 lines. I’m guessing the linecount of the Django version will be somewhere around a third of that. It’s great to see the code melt away, even when it’s just single lines getting shorter. For example, this:

$comments = $blog->query("SELECT,, posts.title, FROM
comments, posts WHERE ORDER BY comments.timestamp DESC 

becomes this:

comments = Comment.objects.all().order_by('-timestamp')[:RECENT_COMMENT_LIMIT]

When I started working with Django, one thing I missed from my own PHP admin code was automatic introspection. For example, in my system, this code produces a complete, functioning admin page for editing comments:

include "DB_Edit.class.php";
$admin = new DB_Edit ($DB_PATH, "comments");
$admin->set_summary_cols ("name", "post_ref", "timestamp");  // like Admin.list_display
$admin->set_input_type ("comment", "textarea");
$body = $admin->html;
print $body;  // typically you would include() a template instead of printing

DB_Edit basically assumes everything is input type="text" unless told otherwise. Not a bad default for most apps, and it makes getting going very quick. But over time, because there’s no single definition of the model (except for the database itself), model logic ends up getting scattered throughout the code. Django is keeping things much cleaner.

Yay Django for using regex-based URL patterns that easily replace mod_rewrite rules. Yay me for having no public *.php URLs in the current version.

This is a rewrite, not a port. Instead of going through my PHP codebase line by line, I’m building up the new version component by component. With the models in place, there’s not much need to refer to the PHP code at all.

More news as it happens.

Matisse Enzer commented on Mon Apr 10 11:35:03 2006:

I especially like the before-and-after comparison - that sort fo thing helps me understand code better than a lot of explanations.

Dagur commented on Mon Apr 10 12:05:27 2006:

comments = Comment.objects.all().order_by('-timestamp')[:RECENT_COMMENT_LIMIT]

Just wondering, isn’t it possible to limit the number of results without fetching all the objects and slicing?

Paul commented on Mon Apr 10 13:12:23 2006:

It’s not only possible, but it’s already happening in that example – the db lookups are lazy. See the source for details if you’re curious.

Adrian Holovaty commented on Mon Apr 10 16:43:05 2006:

Note you don’t need the all() there, although it works either way.





You only need the all() if you’re not applying any filters.

Paul commented on Mon Apr 10 16:50:49 2006:

Thanks, not sure how I made that error. I have the M-R query examples taped to my desk!

Got tags working today. Getting closer…

dp_wiz commented on Tue Apr 11 10:35:03 2006:

When I started working with Django, one thing I missed from my own PHP admin code was automatic introspection.

Duh! Django’s internal admin stuff is really awesome.

Paul commented on Tue Apr 11 10:47:42 2006:

Indeed it is. But it doesn’t to table introspection on the fly. As I said, Django’s approach is ultimately cleaner.

dp_wiz commented on Wed Apr 12 02:58:13 2006:

you can take introspection code from django-admin.

Paul commented on Wed Apr 12 09:10:42 2006:

Perhaps I’m not being clear. I don’t want to change the way Django works!

Andrew commented on Fri Apr 14 01:39:27 2006:

Will your code be available? I’m curious as to how you’re doing tagging. I’ve implemented a very craptastic way to do it for my blog (though i haven’t put it up yet), but really don’t like my solution to it. Glad to see you moving this to django.

Paul commented on Fri Apr 14 09:10:37 2006:

Funny you should mention tagging – that’s the one part of my original model that I’ve decided really needs to change. Django makes it so easy to do many-to-many relations that I can do it right, instead of carrying over the (surprisingly functional) space-delimited-text hack that I’m using now. Having a real tag model will make it easier to reimplement the current features and to add new ones.

I may use Hugo’s tagging code.

I do plan to release the source, but I’m so busy with work that the whole thing’s going in slow-motion right now.

Matthias commented on Sun May 14 18:22:07 2006:

Yay, an open-source Django blog! Can’t wait.