librelist archives

« back to archive

Sharing a model between Flask-SQLAlchemy and other apps

Sharing a model between Flask-SQLAlchemy and other apps

From:
Luca Lesinigo
Date:
2012-06-21 @ 13:46
I already have a python app that reads (via good old SQL and ODBC) some 
data from IBM DB2 and loads it up in a MySQL db via SQLAlchemy. I have my 
good and clean model.py file with everything inside and nothing else.

Now to use the same model.py in a Flask-SQLAlchemy file I just have to 
change an import, to get the Model from Flask-SQLAlchemy instead of 
directly SQLAlchemy. In other words I need to do:
  from myapp import db
  Model = db.Model
  metadata = db.metadata
  # the other stuff I import directly from sqlalchemy / sqlalchemy.orm
( I have the app structured as a python package with db = SQLAlchemy(app) 
in its __init__ )

I could arrange some ugly "if" around the imports to check whether I am 
under plain SQLAlchemy or under Flask-SQLAlchemy, but then again I would 
have to hardcode every Flask app name (from specificappname import db)

In general, how would you go about sharing an SA model between 
Flask-SQLAlchemy and the "outer world"?

If that matters, everything gets installed in a single directory 
containing all our python stuff, so everything is directly and cleanly 
importable like all system-wide deb-managed stuff (we're actually thinking
about packaging our internal stuff in .DEB to ease management, but that's 
another story).
So you can "import mymodel" from any Python script running on the system.

thanks,
--
Luca Lesinigo

Re: [flask] Sharing a model between Flask-SQLAlchemy and other apps

From:
Simon Sapin
Date:
2012-06-23 @ 09:18
Le 21/06/2012 15:46, Luca Lesinigo a écrit :
> I already have a python app that reads (via good old SQL and ODBC) some 
data from IBM DB2 and loads it up in a MySQL db via SQLAlchemy. I have my 
good and clean model.py file with everything inside and nothing else.
>
> Now to use the same model.py in a Flask-SQLAlchemy file I just have to 
change an import, to get the Model from Flask-SQLAlchemy instead of 
directly SQLAlchemy. In other words I need to do:
>    from myapp import db
>    Model = db.Model
>    metadata = db.metadata
>    # the other stuff I import directly from sqlalchemy / sqlalchemy.orm
> ( I have the app structured as a python package with db = 
SQLAlchemy(app) in its __init__ )
>
> I could arrange some ugly "if" around the imports to check whether I am 
under plain SQLAlchemy or under Flask-SQLAlchemy, but then again I would 
have to hardcode every Flask app name (from specificappname import db)
>
> In general, how would you go about sharing an SA model between 
Flask-SQLAlchemy and the "outer world"?
>
> If that matters, everything gets installed in a single directory 
containing all our python stuff, so everything is directly and cleanly 
importable like all system-wide deb-managed stuff (we're actually thinking
about packaging our internal stuff in .DEB to ease management, but that's 
another story).
> So you can "import mymodel" from any Python script running on the system.

Hi,

Without any justification other that "consistency is good", I would 
recommend picking either Flask-SQLAlchemy or "raw" SQLAlchemy, and use 
that everywhere.

Flask-SQLAlchemy probably can work outside of Flask, though I’m not sure 
how. Read the docs and experiment:
http://packages.python.org/Flask-SQLAlchemy/contexts.html
(But note https://github.com/mitsuhiko/flask-sqlalchemy/pull/86/files )

On the other hand, I’ve seen Flask applications where Flask-SQLAlchemy 
didn’t bring much and SQLAlchemy was used directly. Flask-SQLAlchemy is 
a set of convenience helpers. It can make things easier, but it is never 
essential.

Regards,
-- 
Simon Sapin