How can we use ORM query for flask?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



How can we use ORM query for flask?



Is it possible to use ORM in flask, as i am facing the problem with native queries, is there any solution for it.





google.co.uk/search?q=flask+orm
– Charming Robot
1 min ago





1 Answer
1



SQLAlchemy in Flask


SQLAlchemy in Flask



Many people prefer SQLAlchemy for database access. In this case it’s
encouraged to use a package instead of a module for your flask
application and drop the models into a separate module (Larger
Applications). While that is not necessary, it makes a lot of sense.



There are four very common ways to use SQLAlchemy. I will outline each
of them here:



Flask-SQLAlchemy Extension


Flask-SQLAlchemy Extension



Because SQLAlchemy is a common database abstraction layer and object
relational mapper that requires a little bit of configuration effort,
there is a Flask extension that handles that for you. This is
recommended if you want to get started quickly.


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():
# import all modules here that might define models so that
# they will be registered properly on the metadata. Otherwise
# you will have to import them first before calling init_db()
import yourapplication.models
Base.metadata.create_all(bind=engine)



You can find more information about sqlAlchemy here






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

make 2 or more post in bootsrap

Store custom data using WC_Cart add_to_cart() method in Woocommerce 3

Firebase Auth - with Email and Password - Check user already registered