Relative Content

Tag Archive for pythonsqlalchemyflask-sqlalchemy

How to elegantly combine complex Python object and SQLAlchemy object model classes?

I have a rather complex class with complex properties computed from a provided df to init and these properties may be other class types that can be eventually serialized into a string. In Python I want to deal with objects rather than primitive types but also want to use SQLAlchemy for interacting with databases. The columns in the table are the same as many of the class properties, how can I combine these two classes elegantly? I could use composition and have the db model as an object within the class, but that doesn’t feel like truly combining? I’d have to have two separate classes that call each other’s APIs.

Include logic into table class

I use Flask-SQLAlchemy + Flask-Migrate as my database for my website. I prefer declarative style, because I end up with a class, an actual entity that has attributes that I specified: