flask-sqlalchemy

flask sqlalchemy query with keyword as variable

我与影子孤独终老i 提交于 2019-11-27 13:40:57
Let's say I have a model like this: class User(db.Model): id = db.Column(db.Integer, primary_key=True) hometown = db.Column(db.String(140)) university = db.Column(db.String(140)) To get a list of users from New York, this is my query: User.query.filter_by(hometown='New York').all() To get a list of users who go to USC, this is my query: User.query.filter_by(university='USC').all() And to get a list of users from New York, and who go to USC, this is my query: User.query.filter_by(hometown='New York').filter_by(university='USC').all() Now, I would like to dynamically generate these queries based

PyCharm resolving - flask.ext.sqlalchemy vs flask_sqlalchemy

霸气de小男生 提交于 2019-11-27 13:40:05
问题 If I use the following format in my application, everything works, except PyCharms resolving / autocomplete feature: from flask.ext.sqlalchemy import SQLAlchemy If I use the following format in my application, everything works. But, alas, it is not the correct way to import the libraries: from flask_sqlalchemy import SQLAlchemy Is there any way to make PyCharm resolve the first syntax correctly? 回答1: The flask.ext namespace is a transistion namespace, see the Extension Import Transition

SQLAlchemy ordering by count on a many to many relationship

北战南征 提交于 2019-11-27 13:31:35
This is a simplified example of my current models (I'm using the Flask SQLAlchemy extension ) : like = db.Table( 'like', db.Column('uid', db.Integer, db.ForeignKey('users.id')), db.Column('pid', db.Integer, db.ForeignKey('posts.id')) ) class User(db.Model): __tablename__ = 'users' id = db.Column(db.Integer, primary_key = True) username = db.Column(db.String(20)) class Post(db.Model): __tablename__ = 'posts' id = db.Column(db.Integer, primary_key = True) title = db.Column(db.String(255)) likes = db.relationship( 'User', secondary = like, backref = db.backref('likes', lazy = 'dynamic'), lazy =

Python Flask SQLAlchemy Pagination

邮差的信 提交于 2019-11-27 13:09:52
问题 I am having trouble implementing pagination with Flask-SQLAlchemy or Flask-Pagination, either or. I am unsure how to initialize the pagination, setting pages, determining pages, offest, etc. I am coming from PHP, quite new to Python. I am querying all the posts in my database posts = Posts.query.order_by(Posts.time.desc()).all() I have been looking at the following examples: http://www.ergo.io/tutorials/better-pagination-in-flask/better-pagination-in-flask/ https://pythonhosted.org/Flask

Can SQLAlchemy be used with Google Cloud SQL?

拜拜、爱过 提交于 2019-11-27 12:49:24
I've looked over Google Cloud SQL's documentation and various searches, but I can't find out whether it is possible to use SQLAlchemy with Google Cloud SQL, and if so, what the connection URI should be. I'm looking to use the Flask-SQLAlchemy extension and need the connection string like so: mysql://username:password@server/db I saw the Django example, but it appears the configuration uses a different style than the connection string. https://developers.google.com/cloud-sql/docs/django Google Cloud SQL documentation: https://developers.google.com/cloud-sql/docs/developers_guide_python Sean

Circular import of db reference using Flask-SQLAlchemy and Blueprints

眉间皱痕 提交于 2019-11-27 12:03:56
I am using Flask-SQLAlchemy and Blueprints and I cannot help myself from using circular imports. I know I can write imports inside functions and make it work but it sounds nasty, I'd like to confirm with the community if there is a better way to do this. The problem is I have a module (blueprints.py) where I declare the database and import the blueprints but those blueprints need to import the database declaration at the same time. This is the code (excerpt of the important parts): application.apps.people.views.py from application.blueprints import db people = Blueprint('people', __name__,

Getting first row from sqlalchemy

孤街醉人 提交于 2019-11-27 11:26:51
问题 I have the following query: profiles = session.query(profile.name).filter(and_(profile.email == email, profile.password == password_hash)) How do I check if there is a row and how do I just return the first (should only be one if there is a match)? 回答1: Use query.one() to get one, and exactly one result. In all other cases it will raise an exception you can handle: from sqlalchemy.orm.exc import NoResultFound from sqlalchemy.orm.exc import MultipleResultsFound try: user = session.query(User)

Flask-SQLAlchemy how to delete all rows in a single table

百般思念 提交于 2019-11-27 11:01:40
问题 How do I delete all rows in a single table using Flask-SQLAlchemy? Looking for something like this: >>> users = models.User.query.all() >>> models.db.session.delete(users) # but it errs out: UnmappedInstanceError: Class '__builtin__.list' is not mapped 回答1: Try delete: models.User.query.delete() From the docs: Returns the number of rows deleted, excluding any cascades. 回答2: DazWorrall's answer is spot on. Here's a variation that might be useful if your code is structured differently than the

unable to create autoincrementing primary key with flask-sqlalchemy

最后都变了- 提交于 2019-11-27 10:47:04
问题 I want my model's primary key to be an autoincrementing integer. Here is how my model looks like class Region(db.Model): __tablename__ = 'regions' id = db.Column(db.Integer, primary_key=True, autoincrement=True) name = db.Column(db.String(100)) parent_id = db.Column(db.Integer, db.ForeignKey('regions.id')) parent = db.relationship('Region', remote_side=id, primaryjoin=('Region.parent_id==Region.id'), backref='sub-regions') created_at = db.Column(db.DateTime, default=db.func.now()) deleted_at

Flask-SQLalchemy update a row's information

﹥>﹥吖頭↗ 提交于 2019-11-27 10:14:24
How can I update a row's information? For example I'd like to alter the name column of the row that has the id 5. Mark Hildreth Retrieve an object using the tutorial shown in the Flask-SQLAlchemy documentation . Once you have the entity that you want to change, change the entity itself. Then, db.session.commit() . For example: admin = User.query.filter_by(username='admin').first() admin.email = 'my_new_email@example.com' db.session.commit() user = User.query.get(5) user.name = 'New Name' db.session.commit() Flask-SQLAlchemy is based on SQLAlchemy, so be sure to check out the SQLAlchemy Docs as