flask-sqlalchemy

How to use a labelled column in sqlalchemy filter?

牧云@^-^@ 提交于 2019-12-12 19:11:03
问题 I want to use the labelled column in sqlalchemy filter. for eg: db.session.query( PartMaster.name, PartMaster.description, PartTracker.actual_length, func.sum(PartTracker.quantity).label('quantity') ).join(PartTracker).group_by( PartTracker.part_master_id,PartTracker.actual_length ).all() I need the result with quantity > 0 . please advice 回答1: In SQL if you want to filter rows by result, you need to use HAVING instructions. So in your case: db.session.query( PartMaster.name, PartMaster

Flask before_request and teardown_request for DB connections

邮差的信 提交于 2019-12-12 18:40:20
问题 According to the Flaskr tutorial, db connection should be opened and closed before each session: @app.before_request def before_request(): g.db = connect_db() @app.teardown_request def teardown_request(exception): db = getattr(g, 'db', None) if db is not None: db.close() However, this is the case when using sqlite3. my question is: when using postressql and SqlAlchemy - do connections need to be opened and closed in the same way, or does SQLAlchemy takes care of connection management

sqlalchemy: TypeError: unhashable type creating instance, sqlalchemy

大兔子大兔子 提交于 2019-12-12 14:53:25
问题 I am getting an error while trying to update code for: https://github.com/thrisp/flask-security from python 2.7 to 3.3 given the following most basic instance test.py: from flask import Flask from flask.ext.sqlalchemy import SQLAlchemy from flask.ext.security import Security, UserMixin, RoleMixin, \ SQLAlchemyUserDatastore app = Flask(__name__) app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite://' db = SQLAlchemy(app) db.drop_all() roles_users = db.Table('roles_users', db.Column('user_id', db

override relationship behaviour in sqlalchemy

痞子三分冷 提交于 2019-12-12 12:33:56
问题 Say I have three tables in a declarative fashion, Parent , Child , and Pet , in such way that Parent has a many-to-many relationship with both Child and Pet Child has a one-to-many relationship with Pet The code for them is (using Flask-SQLAlchemy, although I believe the solution lives in the realm of SQLAlchemy rather than in Flask). class Parent(db.Model): __tablename__ = 'parents' id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(64)) # many to many relationship

Flask-SQLAlchemy InvalidRequestError: Object is already attached to session

孤者浪人 提交于 2019-12-12 10:54:43
问题 I'm creating a forum project using Flask, and managing all the users, threads, posts, etc. using Flask-SQLAlchemy. However, I've found that when I attempt to do x (e.g. edit a post), I get an InvalidRequestError if I attempt to do anything else (e.g. delete the post). For editing a post, def post_edit(id, t_id, p_id): post = Post.query.filter_by(id=p_id).first() if post.author.username == g.user.username: form = PostForm(body=post.body) if form.validate_on_submit(): post.body = form.body.data

Additional action button doesn't work on flask-admin

扶醉桌前 提交于 2019-12-12 09:55:35
问题 I'm trying to add one more action to flask-admin forms. It has to increment rating (+1) and it works with batch action, but not with single. Please help me find the bug, I've spent a lot of time trying to make this thing work properly. Here's the code: I made an html template in templates folder - custom_lists.html {% extends 'admin/model/list.html' %} {% block list_row_actions %} {{ super() }} <form class="icon" method="POST" action="/admin/user/action/"> <input id="action" name="action"

Flask-sqlalchemy query datetime intervals

只愿长相守 提交于 2019-12-12 09:46:31
问题 I have defined a table with flask-sqlalchemy. Displayed below. class Notes(db.Model): id = db.Column(db.Integer, primary_key=True) notes = db.Column(db.Text, nullable=False) user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False) added_at = db.Column(db.DateTime, default=db.func.now()) @staticmethod def newest(num): return Notes.query.order_by(desc(Notes.added_at)).limit(num) I'm attempting to write a query that is to replace and already existing direct query, which looks

Querying from list of related in SQLalchemy and Flask

余生颓废 提交于 2019-12-12 08:42:44
问题 I have User which has-one Person . So User.person is a Person . I am trying to get a list of User from a list of Person . I tried the following: >>> people = Person.query.filter().limit(3) <flask_sqlalchemy.BaseQuery object at 0x111c69bd0> >>> User.query.filter(User.person.in_(people)).all() NotImplementedError: in_() not yet supported for relationships. For a simple many-to-one, use in_() against the set of foreign key values. What is the best way to get a list of User s where User.person is

SQLAlchemy chaining association proxy for great grandchildren?

主宰稳场 提交于 2019-12-12 08:27:52
问题 I have a four classes like so: Group , Parent , Child , Toy . Group has a parents relationship pointing to Parent Parent has a children relationship pointing to Child Child has a toys relationship pointing to Toy Parent has a toys association_proxy that produces all the Toy s that the Parent 's children have. I want to be able to get all the Toys in a Group. I tried to create an association_proxy on Group that links to Parent 's toys , but it produces this: [[<Toy 1>, <Toy 2>], [], [], []]

Unable to create models on Flask-admin

梦想的初衷 提交于 2019-12-12 07:32:09
问题 I'm creating a simple blog on Flask and I'm trying to implement Flask-Admin to manage my posts. If I go to the admin area I can see a list of all my post from the DB but when I try to create a new one I got the next error: Failed to create model. __init__() takes exactly 4 arguments (1 given) This is my post model: class Post(db.Model): __tablename__ = 'news' nid = db.Column(db.Integer, primary_key = True) title = db.Column(db.String(100)) content = db.Column(db.Text) created_at = db.Column