uralbash / sqlalchemy_mptt

SQLAlchemy nested sets mixin (MPTT)
http://sqlalchemy-mptt.readthedocs.io
MIT License
196 stars 32 forks source link

Automatically register tree classes #28

Closed GaretJax closed 10 years ago

GaretJax commented 10 years ago

Tree classes can automatically be registered by adding the following code to the mixin class:

@classmethod
def __declare_last__(cls):
    cls.register_tree()

This would avoid to have to call Tree.register_tree() after each declaration of Tree.

GaretJax commented 10 years ago

Thinking this through, it may be better to use the after_configured event directly. The advantage would be that a subclass could overwrite this method without having to register the tree (or calling super) themselves.

uralbash commented 10 years ago

I do not quite understand how to implement it using after_configured event. Can you show some example?

GaretJax commented 10 years ago

Just ended up implementing it myself. I actually never used that event before, so I had to dig in the code anyway.

uralbash commented 10 years ago

I tested it on my projects. This solution works fine, thank you!

GaretJax commented 10 years ago

There may be an even better solution I just came across: http://docs.sqlalchemy.org/en/latest/changelog/migration_08.html?highlight=propagate#events-can-be-applied-to-unmapped-superclasses