librelist archives

« back to archive

Struggling with Modules and Flask-sqlalchemy

Struggling with Modules and Flask-sqlalchemy

From:
Oliver Andrich
Date:
2010-06-21 @ 15:08
Hi everybody,

I have run into a circular import issue, which I can solve by falling back
to pure SQLAlchemy instead of Flask-sqlalchemy. But honestly I would love to
use this nice extension. But let me describe my problems, may be I just
missing something. May be it's the
exhilaration</englisch-deutsch/exhilaration.html> after
launching my first Flask today. :)

I have a *__init__.py* file which defines the app and db object.

app = Flask(__name__)
app.register_module(auth, url_prefix='/auth')
# ... loading config and so ...
db = SQLAlchemy(app)

I have a models.py file which defines a User class somewhat like that.

import db

# ... definition of BaseMixin, and so on ...

class User(db.Model, BaseMixin):
    __tablename__           = 'cm_users'

    user_name               = db.Column(db.String(80), unique=True,
nullable=False)

And I have the auth module living in the file views/auth.py

from functools import wraps
from flask import Module, request, session, redirect, url_for,
render_template, g
from myapp.models import User

auth = Module(__name__)

@auth.before_app_request
def lookup_current_user():
    g.user = None
    if 'cm_username' in session:
        g.user = User.query.filter_by(user_name=session['cm_username'])

This can't work, which is obvious to me, cause __init__.py imports the auth
module in order to register the module. The auth module imports the models
module to access the model class User. Is there a solution for this?

Best regards,
Oliver

Re: [flask] Struggling with Modules and Flask-sqlalchemy

From:
Dan Jacob
Date:
2010-06-21 @ 15:11
In your models.py:

from flaskext.sqlalchemy import SQLAlchemy

db = SQLAlchemy()

class User(db.Model):
    .....

In your __init__.py:

from models import db, User

app = Flask(__name__)
db.init_app(app)



On 21 June 2010 16:08, Oliver Andrich <oliver@2pxnr.de> wrote:
> Hi everybody,
> I have run into a circular import issue, which I can solve by falling back
> to pure SQLAlchemy instead of Flask-sqlalchemy. But honestly I would love to
> use this nice extension. But let me describe my problems, may be I just
> missing something. May be it's the exhilaration after launching my first
> Flask today. :)
> I have a __init__.py file which defines the app and db object.
> app = Flask(__name__)
> app.register_module(auth, url_prefix='/auth')
> # ... loading config and so ...
> db = SQLAlchemy(app)
> I have a models.py file which defines a User class somewhat like that.
> import db
> # ... definition of BaseMixin, and so on ...
> class User(db.Model, BaseMixin):
>     __tablename__           = 'cm_users'
>
>     user_name               = db.Column(db.String(80), unique=True,
> nullable=False)
> And I have the auth module living in the file views/auth.py
> from functools import wraps
> from flask import Module, request, session, redirect, url_for,
> render_template, g
> from myapp.models import User
> auth = Module(__name__)
> @auth.before_app_request
> def lookup_current_user():
>     g.user = None
>     if 'cm_username' in session:
>         g.user = User.query.filter_by(user_name=session['cm_username'])
> This can't work, which is obvious to me, cause __init__.py imports the auth
> module in order to register the module. The auth module imports the models
> module to access the model class User. Is there a solution for this?
> Best regards,
> Oliver

Re: [flask] Struggling with Modules and Flask-sqlalchemy

From:
Oliver Andrich
Date:
2010-06-21 @ 16:10
Hi Dan,

thanks a lot. This works now and as I expected it. Another lesson learned
about Flask. The only other change I had to apply was, that I had to rewrite
my action_db_create_all() management command in such a way, that before
calling db.create_all() a request_context has to be created.

I hope it is okay and save to do that.

def action_db_create_all():
    """Create all database tables."""
    from myapp.models import User
    ctx = app.test_request_context()
    ctx.push()
    db.create_all()
    ctx.pop()

Best regards,
Oliver

2010/6/21 Dan Jacob <danjac354@gmail.com>

> In your models.py:
>
> from flaskext.sqlalchemy import SQLAlchemy
>
> db = SQLAlchemy()
>
> class User(db.Model):
>    .....
>
> In your __init__.py:
>
> from models import db, User
>
> app = Flask(__name__)
> db.init_app(app)
>
>
>
> On 21 June 2010 16:08, Oliver Andrich <oliver@2pxnr.de> wrote:
> > Hi everybody,
> > I have run into a circular import issue, which I can solve by falling
> back
> > to pure SQLAlchemy instead of Flask-sqlalchemy. But honestly I would love
> to
> > use this nice extension. But let me describe my problems, may be I just
> > missing something. May be it's the exhilaration after launching my first
> > Flask today. :)
> > I have a __init__.py file which defines the app and db object.
> > app = Flask(__name__)
> > app.register_module(auth, url_prefix='/auth')
> > # ... loading config and so ...
> > db = SQLAlchemy(app)
> > I have a models.py file which defines a User class somewhat like that.
> > import db
> > # ... definition of BaseMixin, and so on ...
> > class User(db.Model, BaseMixin):
> >     __tablename__           = 'cm_users'
> >
> >     user_name               = db.Column(db.String(80), unique=True,
> > nullable=False)
> > And I have the auth module living in the file views/auth.py
> > from functools import wraps
> > from flask import Module, request, session, redirect, url_for,
> > render_template, g
> > from myapp.models import User
> > auth = Module(__name__)
> > @auth.before_app_request
> > def lookup_current_user():
> >     g.user = None
> >     if 'cm_username' in session:
> >         g.user = User.query.filter_by(user_name=session['cm_username'])
> > This can't work, which is obvious to me, cause __init__.py imports the
> auth
> > module in order to register the module. The auth module imports the
> models
> > module to access the model class User. Is there a solution for this?
> > Best regards,
> > Oliver
>