Saturday, January 12, 2008

Deeply Nested Resources in Pylons

I've finally got around to working out how to achieve deeply nested resources in Pylons and Routes.

Say you want a URL like this:

/books/some_book/chapters/some_chapter/pages/some_page

where 'some_book', 'some_chapter', 'some_page' are member names you want to fetch out of the 'book', 'chapters' and 'pages' collections.

Using routes, you cannot achieve this using the 'parent_resource' option only. You need something extra. The config/routes.py file will need to have these entries:
map.resource('book', 'books')
map.resource('chapter', 'chapters', parent_resource=dict(member_name='book', collection_name='books'))
map.resource('page', 'pages',
path_prefix = '/books/:book_id/chapters/:chapter_id',
name_prefix = 'page_'
)
The route_dict dictionary still has all the information you need. I haven't tried this beyond three resources.

1 comment:

TheseThingsNeedFixed said...

Just what I was looking for!

Popular Posts