On Fri, Feb 17, 2012 at 01:42:04AM +0400, Andrey Popp wrote: > On Thu, Feb 16, 2012 at 12:09:36PM -0800, Mike Orr wrote: > > Second, how can I generate URLs to resources without loading the objects > > through this interface? The home page queries the 30 most recent incidents > > and displays links to them. request.resource_url() requires a resource > > object, but the home page or browse pages query the database directly and > > get > > plain ORM objects rather than location-aware ones. I could make the URLs > > manually but that sucks. I could somehow get the "incident" resource and > > append the ID, or get the root resource and append "incident" and the ID, > > but > > that's not much better. > > That's interesting question. I'm thinking of the following construct: > > 1. Make your resource graph lazy, e.g. producing only proxies which only > knows its location (pk in database in case of SQLAlchemy) and how to load > itself (table and parent resources, which impose some query constraints or > JOIN clauses). > > 2. Reuse your resource graph in pyramid's traversal engine and in your home > page view: > > def homepage(): > entries_ids = db.entries_ids_for_homepage() > entries = [root["entries"][id] for id in entries_ids] > > So you get ``entries`` list of location-aware Entry objects which are loaded > only if you access their fields, but not __name__ or __parent__ which allows > you to generate URL for them w/o any overhead.
If someone is still interested in this approach, I've implemented[1] sqlalchemy.Query.lazy_get(ident) method which works that way. My use-case is different -- to allow User object to be attached on request without touching database (user id is encoded in authc cookie), but I think it should work for constructing lazy resource graph as well. Implementation based on SQLAlchemy mailing list post[2]. [1]: https://gist.github.com/77eb31670a1842f301c2 [2]: https://groups.google.com/forum/?fromgroups#!topic/sqlalchemy/DvofQn6uVI4 -- You received this message because you are subscribed to the Google Groups "pylons-discuss" group. To post to this group, send email to pylons-discuss@googlegroups.com. To unsubscribe from this group, send email to pylons-discuss+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/pylons-discuss?hl=en.