Jared Nuzzolillo wrote: > Greetings! > > I've known about sqlalchemy for a long time and have been interested in > using it professionally for a while now. I finally have the chance to use > it > and have really been enjoying it. But it turns out that I am a little > confused about the best way to handle m2m relationships that have > additional > fields. > > Let's say we have 4 tables, User, Tag, Post, PostTagAssociation. > PostTagAssociation has FKs of course, but also a tagged_at datetime field. > > I am familiar with association_proxy, but I prefer a different sort of > interface. Something closer to dynamic_loader that would let me do things > like: > > #querying across my dynamic_loader: > post = test_user.posts.filter(Post. > title.like('%geopolitic%')).first() > #query through an association proxy, eg to get all posts tagged with war > related keywords: > #of course, this doesn't work since it returns an AssociationList instead > of > an AppenderQuery: > post.tags.filter(Tag.name.like('%war%')) > #and then i still want to be able to have the AssociationList > functionality > like: > post.tags.append(Tag("peace")) > post.tags.remove(Tag("war")) > #what would be really amazing is if I could set it up to decorate > #the returned item with some or all fields from the m2m table, eg: > print post.tags.first().tagged_at # => 10/27/2009 21:24 > > This seems like it would be commonly desired functionality, so I am > wondering if perhaps: a) I missed it, and there is some option already in > sqlalchemy, or b) Someone already has created an extension that provides > this. > > If not, I wonder if supplying a new query class to the query_class option > of > dynamic_loader might be able to accomplish this.
I'm assuming Post.tags is an associationproxy. So dynamic_loader isn't used here (that only applies to a relation()), you'd be looking to build an associationproxy that produces a Query() when called upon. So you really want to build an associationproxy._AssociationCollection subclass that does what you'd like, and specify it using proxy_factory and proxy_bulk_set on the associationproxy. Because the proxied collection lives outside of what the unit of work cares about, it doesn't need all the weird stuff that dynamic_loader has to do in order to work with the ORM, so should be fairly straightforward to create. Does that strategy make > sense? Are there any examples around that supply a query_class to the > dynamic_loader function? I couldn't find any, though I definitely might > have > missed them. > > I know it's also possible that am thinking about this all wrong, asking > the > wrong question since I am used to other kinds of ORMs. If so, I'd love to > hear how experienced sqlalchemy users achieve this same sort of > functionality (eg, do they manually add methods to their model classes > that > work directly with the association table and do a subquery, or something > completely different?) > > Thank you so very much, and please excuse the long post. > > Best! > Jared Nuzzolillo > > > > --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "sqlalchemy" group. To post to this group, send email to sqlalchemy@googlegroups.com To unsubscribe from this group, send email to sqlalchemy+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en -~----------~----~----~----~------~----~------~--~---