Hi,
I use the init_app() method in order to setup the SQLAlchemy extension, but it
do not run.
I've a particular system.
== creator.py ==
from flask import Flask
def create_app(import_name, config_filename):
app.config.from_object(config_filename)
return app
== manage.py ==
from creator import create_app
db = SQLAlchemy()
if __name__ == '__main__':
app = create_app(__name__, 'settings')
db.init_app(app)
app.run()
If I use the "db" variable I see that the SQLAlchemy is not initialized and,
using the db.engine, I get this error:
RuntimeError: application not registered on db instance and no application
bound to current context
I fix it in this way that is not documented (manage.py file):
...
app = create_app(__name__, 'settings')
db.app = app # <- added this line
db.init_app(app)
...
I think that the db.init_app() method is not complete, or I misunderstood
something?
Davide Muzzarelli
www.dav-muz.net
This page may be helpful http://flask.pocoo.org/mailinglist/archive/2010/8/12/model-scattered-between-files/. 2010/8/30 Davide Muzzarelli <d.muzzarelli@dav-muz.net> > Hi, > I use the init_app() method in order to setup the SQLAlchemy extension, but > it > do not run. > > I've a particular system. > > == creator.py == > from flask import Flask > > def create_app(import_name, config_filename): > app.config.from_object(config_filename) > return app > > == manage.py == > from creator import create_app > > db = SQLAlchemy() > > if __name__ == '__main__': > app = create_app(__name__, 'settings') > db.init_app(app) > app.run() > > > If I use the "db" variable I see that the SQLAlchemy is not initialized > and, > using the db.engine, I get this error: > RuntimeError: application not registered on db instance and no application > bound to current context > > I fix it in this way that is not documented (manage.py file): > ... > app = create_app(__name__, 'settings') > db.app = app # <- added this line > db.init_app(app) > ... > > > I think that the db.init_app() method is not complete, or I misunderstood > something? > > Davide Muzzarelli > www.dav-muz.net >
In data lunedì 30 agosto 2010 17:03:27, heww0205 ha scritto: > This page may be helpful > http://flask.pocoo.org/mailinglist/archive/2010/8/12/model-scattered-betwee > n-files/. Thank you, I do it but it do not run. The example: == mymodule/models.py == from manage import db print 'MODELS: ', db == manage.py == from frawz import create_app from flaskext.sqlalchemy import SQLAlchemy db = SQLAlchemy() if __name__ == '__main__': app = create_app(__name__, 'settings') db.app = app db.init_app(app) print 'MANAGE: ', db from mymodule import models app.run() == output == MANAGE: <SQLAlchemy engine='postgres:///database'> MODELS: <SQLAlchemy engine=None> I think that there is a stupid little thing that I forgot... Davide Muzzarelli www.dav-muz.net
What about this project http://bitbucket.org/danjac/newsmeme ? In this project, it uses Flask-Script, Flask-SQLAlchemy, Flask-WTF extensions and so on. 2010/8/30 Davide Muzzarelli <d.muzzarelli@dav-muz.net> > In data lunedì 30 agosto 2010 17:03:27, heww0205 ha scritto: > > This page may be helpful > > > http://flask.pocoo.org/mailinglist/archive/2010/8/12/model-scattered-betwee > > n-files/. > > Thank you, I do it but it do not run. > > The example: > > > == mymodule/models.py == > from manage import db > print 'MODELS: ', db > > == manage.py == > from frawz import create_app > from flaskext.sqlalchemy import SQLAlchemy > > db = SQLAlchemy() > > if __name__ == '__main__': > app = create_app(__name__, 'settings') > db.app = app > db.init_app(app) > print 'MANAGE: ', db > from mymodule import models > app.run() > > == output == > > MANAGE: <SQLAlchemy engine='postgres:///database'> > MODELS: <SQLAlchemy engine=None> > > > I think that there is a stupid little thing that I forgot... > > Davide Muzzarelli > www.dav-muz.net >
In data lunedì 30 agosto 2010 17:34:36, heww0205 ha scritto: > What about this project http://bitbucket.org/danjac/newsmeme ? > > In this project, it uses Flask-Script, Flask-SQLAlchemy, Flask-WTF > extensions and so on. Thank you, that is very close to my system! I written this and run, but I do not know if it have concurrency problems and why the previous code do not work... == creator.py == from flask import Flask from flaskext.sqlalchemy import SQLAlchemy def create_app(import_name, config_filename): app.config.from_object(config_filename) global db db = SQLAlchemy(app) import models return app == manage.py == from creator import create_app if __name__ == '__main__': app = create_app(__name__, 'settings') app.run() == models.py == from creator import db class User(db.Models): ... Davide Muzzarelli www.dav-muz.net
What I recommend is having your SQLAlchemy instance in a 3rd file, for example extensions.py: from flaskext.sqlalchemy import SQLAlchemy db = SQLAlchemy() Then you can import it into the module where you set up your app, and your models file(s) without any circular import issues: from myapp.extensions import db app = Flask(__name__) db.init_app(app) I'm not sure what you mean by "concurrency problems". Flask-SQLAlchemy uses the SQLAlchemy scoped_session which creates a per-thread session: http://www.sqlalchemy.org/docs/reference/orm/sessions.html?highlight=scoped_session#sqlalchemy.orm.scoped_session It also ensures that the session is removed at the end of the request. On 30 August 2010 16:47, Davide Muzzarelli <d.muzzarelli@dav-muz.net> wrote: > In data lunedì 30 agosto 2010 17:34:36, heww0205 ha scritto: >> What about this project http://bitbucket.org/danjac/newsmeme ? >> >> In this project, it uses Flask-Script, Flask-SQLAlchemy, Flask-WTF >> extensions and so on. > > Thank you, that is very close to my system! > > I written this and run, but I do not know if it have concurrency problems and > why the previous code do not work... > > == creator.py == > from flask import Flask > from flaskext.sqlalchemy import SQLAlchemy > > def create_app(import_name, config_filename): > app.config.from_object(config_filename) > global db > db = SQLAlchemy(app) > import models > return app > > == manage.py == > from creator import create_app > > if __name__ == '__main__': > app = create_app(__name__, 'settings') > app.run() > > > == models.py == > from creator import db > > class User(db.Models): > ... > > > Davide Muzzarelli > www.dav-muz.net >
In data martedì 31 agosto 2010 18:28:22, Dan Jacob ha scritto:
> What I recommend is having your SQLAlchemy instance in a 3rd file ...
I do that and it run. I do not understand why the way in 2 files have
problems...
Thanks Dan.
Davide Muzzarelli
www.dav-muz.net