Final version: def walk_related(obj, enableDuplicates=False, level=-1, memo=None): memo = memo or set() insp = inspect(obj) for relobj in insp.mapper.cascade_iterator('delete', insp): if enableDuplicates or relobj[0] not in memo: memo.add(relobj[0]) yield level, relobj[0] relatedObjects = walk_related(relobj[0], enableDuplicates=enableDuplicates, level=level + 1, memo= memo) for walked_relobj in relatedObjects: yield walked_relobj
Dne pondělí 14. září 2015 11:57:05 UTC+2 Pavel S napsal(a): > > Hi, > > I just realized that I need your first solution, since I need to get only > those objects that would cascade in case of deletion. > > But thanks anyhow... > > P > > Dne čtvrtek 10. září 2015 15:35:39 UTC+2 Michael Bayer napsal(a): >> >> >> >> On 9/10/15 8:48 AM, Pavel S wrote: >> >> Let's say, I have declarative classes A, B, C, D. >> >> A is the parent >> B has FK&relationship to A >> C has FK&relationship to B, >> D has FK&relationship to C etc... >> >> I'd like to implement *generic method* walk(obj) which will recursively >> yield dependent/related objects of obj (which is instance of A). >> >> I know that there is introspection interface inspect(), however I'm >> don't really understand how to use it properly in my use case. >> >> Shall I do inspect(obj) or rather inspect(obj.__class__) and then >> somehow apply inspection to obj? >> >> Are there an examples and best practices? >> >> right now you can kind of get this effect using cascade_iterator: >> http://docs.sqlalchemy.org/en/rel_1_0/orm/mapping_api.html?highlight=cascade_iterator#sqlalchemy.orm.mapper.Mapper.cascade_iterator >> >> the limitation is that right now its based on relationship cascade >> settings, as that's what it was intended for, so you'd probably want to use >> "save-update": >> >> insp = inspect(my_object) >> for obj in insp.mapper.cascade_iterator("save-update", insp): >> # ... >> >> to implement your own system, the graph of objects is strictly based on >> relationship. so walk() is pretty simple: >> >> def walk(obj): >> yield obj >> insp = inspect(obj) >> for relationship in insp.mapper.relationships: >> related = getattr(obj, relationship.key) >> if relationship.uselist: >> for collection_member in related: >> for walk_related in walk(collection_member): >> yield walk_related >> elif related is not None: >> for walk_related in walk(related): >> yield walk_related >> >> >> >> >> >> >> -- >> You received this message because you are subscribed to the Google Groups >> "sqlalchemy" group. >> To unsubscribe from this group and stop receiving emails from it, send an >> email to sqlalchemy+...@googlegroups.com. >> To post to this group, send email to sqlal...@googlegroups.com. >> Visit this group at http://groups.google.com/group/sqlalchemy. >> For more options, visit https://groups.google.com/d/optout. >> >> >> -- You received this message because you are subscribed to the Google Groups "sqlalchemy" group. To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+unsubscr...@googlegroups.com. To post to this group, send email to sqlalchemy@googlegroups.com. Visit this group at http://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/d/optout.