Converted to support Django 1.3, with design changes from @mtorourk