librelist archives

« back to archive

Flask Model Extension

Flask Model Extension

From:
Date:
2010-09-28 @ 10:20
Hi,

I read Flask, Flask-SQLAlchemy, Flask-CounchDBKit and Flask-CounchDB  
document and examples. But I can't understand main idea behind  
database extesions.

Flask-SQLAlchemy has db.Model stucture. Flask-CouchDb and  
Flask-CounchDBKit has Document stucture. This make me confused. I  
cannot unserstand what Flask need from Model stucture, which  
initialization process must.

App without Flask-SQLAlchemy:

from sqlalchemy import create_engine
from sqlalchemy.orm import scoped_session, sessionmaker
from sqlalchemy.ext.declarative import declarative_base

engine = create_engine('sqlite:////tmp/test.db', convert_unicode=True)
db_session = scoped_session(sessionmaker(autocommit=False,
                                          autoflush=False,
                                          bind=engine))
Base = declarative_base()
Base.query = db_session.query_property()

def init_db():
     Base.metadata.create_all(bind=engine)


App with Flask-SQLAlchemy:

from datetime
import datetimefrom flask import Flask, request, flash, url_for,  
redirect, \     render_template, abort
from flaskext.sqlalchemy import SQLAlchemy

app = Flask(__name__)
app.config.from_pyfile('hello.cfg')
db = SQLAlchemy(app)

I can make some inference from above, but it is not enough. There is  
no CouchDb app example without Flask-CouchDB. If there is, it is very  
helpful.


In the end, I want to make a Model extension for Flask like  
Flask-DataStore or Flask-ZODB. I look forward Flask community support.

Thanks


Re: [flask] Flask Model Extension

From:
Dan Jacob
Date:
2010-09-28 @ 10:30
On 28 September 2010 11:20,  <yilmazmehmet@itu.edu.tr> wrote:
> Hi,
>
> I read Flask, Flask-SQLAlchemy, Flask-CounchDBKit and Flask-CounchDB
> document and examples. But I can't understand main idea behind
> database extesions.

To make it as easy as possible to configure your database with your Flask app.


>
> Flask-SQLAlchemy has db.Model stucture. Flask-CouchDb and
> Flask-CounchDBKit has Document stucture. This make me confused. I
> cannot unserstand what Flask need from Model stucture, which
> initialization process must.

CouchDB is a document database, so has the concept of a "Document".
ORMs such as SQLAlchemy use the concept of a Model which is mapped to
a database relation. Different databases, different concepts.

>
> App without Flask-SQLAlchemy:
>
> from sqlalchemy import create_engine
> from sqlalchemy.orm import scoped_session, sessionmaker
> from sqlalchemy.ext.declarative import declarative_base
>
> engine = create_engine('sqlite:////tmp/test.db', convert_unicode=True)
> db_session = scoped_session(sessionmaker(autocommit=False,
>                                          autoflush=False,
>                                          bind=engine))
> Base = declarative_base()
> Base.query = db_session.query_property()
>
> def init_db():
>     Base.metadata.create_all(bind=engine)
>
>
> App with Flask-SQLAlchemy:
>
> from datetime
> import datetimefrom flask import Flask, request, flash, url_for,
> redirect, \     render_template, abort
> from flaskext.sqlalchemy import SQLAlchemy
>
> app = Flask(__name__)
> app.config.from_pyfile('hello.cfg')
> db = SQLAlchemy(app)
>
> I can make some inference from above, but it is not enough.

What is enough ? The SQLAlchemy extension handles all that
configuration for you. It also ensures that the session is removed at
the end of each request, and conveniently wraps the SQLAlchemy API in
a single object.

Does that cover every possible situation ? Probably not, but then you
can subclass or patch the extension if you need to, or discard it
altogether. It should cover the common case quite nicely.

> There is
> no CouchDb app example without Flask-CouchDB. If there is, it is very
> helpful.
>

Not sure what you mean.

Re: [flask] Flask Model Extension

From:
Date:
2010-09-28 @ 11:11
Thanks for your quick answer Dan.

>> There is
>> no CouchDb app example without Flask-CouchDB. If there is, it is very
>> helpful.
>>
>
> Not sure what you mean.
>
>

I'm sorry for my insufficient English. I mean that an app example with  
CouchDB which don't use Flask-CouchDB. In this way, I hope  
understanding better what Flask-CouchDB do.

>
> CouchDB is a document database, so has the concept of a "Document".
> ORMs such as SQLAlchemy use the concept of a Model which is mapped to
> a database relation. Different databases, different concepts.
>

Different databases work differently, but I think all of them should  
make same things in basic. Actually Dan answered my question :

>
> What is enough ? The SQLAlchemy extension handles all that
> configuration for you. It also ensures that the session is removed at
> the end of each request, and conveniently wraps the SQLAlchemy API in
> a single object.
>

I wonder Model extension's working steps. Could you explain this more  
detailed. ?

Thanks

Re: [flask] Flask Model Extension

From:
Daniel Neuhäuser
Date:
2010-09-28 @ 11:28
You could just read the documentation and the source code of those
extensions to see what they are actually doing and how you would do that
without these extensions.