Hi all,
I've asked this question on stackoverflow:
http://stackoverflow.com/questions/12031861/sqlalchemy-how-do-i-get-an-object-from-a-relationship-by-objects-pk/12032405
Basically, I'm looking for a dict-like access to a relationship to be able
to quickly retrieve items by some key (item's PK). To illustrate, a
"normal" relationship is list-like:
for book in library.books:
print book.id # prints 10, 20, 30
*In addition,* I'd like to be able to access "books" from an already-loaded
relationship by their id:
book1 = library.books.by_id(10)
book2 = library.books.by_id(20)
book3 = library.books.by_id(23) # raises KeyError
while still being able to iterate over library.books in a list-like manner.
I'm looking at MappedCollection
(http://docs.sqlalchemy.org/en/latest/orm/collections.html?highlight=collection#custom-dictionary-based-collections)
- would building a custom collection class be the right solution to the
problem? Or is there something pre-built for this purpose in the depths of
SQLAlchemy?
Thanks,
--
You received this message because you are subscribed to the Google Groups
"sqlalchemy" group.
To view this discussion on the web visit
https://groups.google.com/d/msg/sqlalchemy/-/mUIFlVguh34J.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/sqlalchemy?hl=en.