16
May 08

Book Review: Learning Website Development with Django

Reviewing The Definitive Guide To Django a few months ago I noted that the key place that book lacked was in examples. As befits the work of the creators of a framework, it did very well at explaining the underlying philosophies and working through all manner of implementation details, but it wasn’t the book for those who just want to dive in and build something. If that’s how you like to use technical books, then Learning Website Development With Django may be more what you’re looking for.

Following the iterative development of a delicious/digg hybrid social bookmarking application, Ayman Hourieh’s book moves quickly through a range of Django features, from setting up your initial models, and using the built in user and admin sections, to supporting AJAX with jQuery, speeding up your app with caching and (briefly) writing automated tests. The pace is fairly measured and Ayman Hourieh does a good job of explaining what’s going on at each step. An experienced web developer should find most of the information they need to get up and running with django, ready to get to work on their own apps.

Perhaps appropriately, where this book is lacking is in explaining how the different parts of the framework fit together. There’s plenty you can pick up by inference, but there are no detailed explanations of, say, the routing system that maps URLs to code. This book’s weaknesses are the former volumes strengths, and while you’ll find much repeated between them a combination of the two is likely to be a good way to get a fully rounded sense of what django is and how you can use it.

Disclaimer: I was sent a copy of this book for review by the publisher. You can find it at packt, amazon US, amazon UK and all sorts of other places.


22
Feb 08

Book Review: The Definitive Guide to Django

Book Sleeve: The Definitive Guide to DjangoDjango is a framework I’ve long (in web years) held in some esteem, despite never having used it before the past few weeks. The framework’s creators’ many well reasoned contributions on all manner of debates about the web suggested a thoughtful approach, and the range of high quality sites powered by Django has kept growing, with the recent launch of EveryBlock being a prime example of its capabilities. So I was delighted to receive a copy of The Definitive Guide to django: Web Development Done Right for review.

Authored by two of the creators of Django: Adrian Holovaty and Jacob Kaplan-Moss, the book is carefully structured, initially placing django in context by exploring various approaches to web development, and then stepping through initial project creation, templates, models, url configuration, django’s famed admin interface, and so on. After eight chapters it changes tack and switches from basic tutorial to more in-depth exploration of areas like the ORM, session handling, caching and deployment. Several appendices provide supplementary material.

The first few chapters do a good job of laying out the foci of the framework’s architecture and it’s Model Template View (MTV) approach. Its pace is measured and while I wonder if it might be a little too much too soon for those totally new to full stack web frameworks, it would work well for those coming from a background building web apps with PHP, Java, or for those of us who are used to working with Rails. There are new techniques to learn and I found the book particularly useful for grasping the deeply pythonic approach, favouring flexibility over convention.

A clear example of that comes in the use of Context objects for passing values between Views and Templates. The authors initially show us the most verbose and rudimentary way to do it and gradually develop that to show how they’ve provided for various common cases. By going through those steps there’s a good chance the reader will be well equipped to work out ways to simplify their own workflow and/or create new subclasses to promote reuse in their code.

Where the book is lacking is in the examples. The introductory material, and much of the reference content is excellent, but as a newcomer to the framework I felt a little lost in how I should structure my code and how different components relate; it’s clear how models relate to the database, but how do I pass them around when building associations?

I recognise that django deliberately avoids the strong conventions of the Rails community (though even there you frequently find newcomers unsure where in the directory structure to place certain components) and there’s no need for lengthy tutorials on building a shop or how a magazine cms could work, but when I come to a book like this I’m looking for a guide to best practices at the project architecture level, not just the component level, and I was sorry not to find it. The authors clearly have a lot of experience of structuring django sites of all shapes and sizes and it would be good to learn more about how they keep those sites organised.

That said, this is a solid introduction to django for web developers; a solid contribution in a so-far underserved market and it’s likely to come in handy for a number of people. Just be prepared to supplement it with a fair amount of time in search engines working out good strategies for connecting the pieces.

Disclaimer: I was sent a copy of this book for review by the publisher. You can read it for free online or find it at apress, amazon US, amazon UK and all sorts of other places.


18
Feb 08

Using the Django ORM as a standalone component

Django logoI’ve found myself working in Python lately, both for a new project and while preparing a review of the Django book. Working in Ruby I’ve become used to relying on ActiveRecord whenever I need to talk to a database (whether inside Rails or not) and after a little time refamiliarising myself with MySQLdb I realised I was going to need a decent ORM for this project. As well as being road-tested and well documented, I was also looking for something that could either generate its models on the fly, or had a tool to generate them from an existing schema.

I checked out SQLAlchemy and SQLObject, both of which looked like worthy contendors, but couldn’t find the generator I was looking for (if you know of one, please do let me know in the comments!) so I switched over to Django. I couldn’t find much information on using their ORM standalone, so thought I should share what I discovered. Please bear in mind that my python is rather rusty–if there are better ways to do any of this I’d be very pleased to hear about it.

The tool to generate your model classes from an existing database is django-admin.py which should be in your path if you’ve got Django installed. To get that up and running you’re going to need to set a few options.

First up, you’ll need a settings.py file specifying your database details. Mine looks like:

DATABASE_ENGINE = 'mysql'
DATABASE_NAME = 'mydatabase'
DATABASE_USER = 'myusername'
DATABASE_PASSWORD = 'mypassword'

Once that is done and saved, then from the command line you should be able to call:

django-admin.py inspectdb --settings=settings

and the models will be echoed to stdout. To redirect that to a file called models.py you’ll need:

django-admin.py inspectdb --settings=settings > models.py

I found a few areas where the generated objects threw errors. We have a column called ‘try’ which is a python keyword, so that required a small change to the code. And a number of models have foreign key relationships with other models that are declared after them, so a little reorganising was called for. It’d be really nice to have the script handle that, but it wasn’t a big deal to make the changes by hand.

With that done, I tried a very simple new script:

from models import *
a = MyModel.objects.all()
print a

But ran into a few issues:

The ORM is relying on the environment variable DJANGO_SETTINGS_MODULE to tell it where to find your database credentials. You’ll need to set that to point to the settings.py file you created earlier.

You can’t have your models.py file in the same folder as your test script. Doing so throws a “IndexError: list index out of range” error. Instead make a new folder called, say, orm and put models.py in it, along with an empty file called __init__.py which will tell python to treat the folder as a module. You can then update your test script to:

from orm.models import *
a = MyModel.objects.all()
print a

With that done, you should be able to use the ORM just as you would in your Django view code.