flask-sqlalchemy

Bulk saving complex objects SQLAlchemy

孤街醉人 提交于 2019-12-17 20:10:18
问题 association_table = Table("association_table", Base.metadata, Column("show_id", Integer(), ForeignKey("show_times.id"), primary_key=True), Column("theater_id", Integer(), ForeignKey("theaters.id"))) association_table2 = Table("association_table2", Base.metadata, Column("show_id", Integer(), ForeignKey("show_times.id"), primary_key=True), Column("movie_id", Integer(), ForeignKey("movies.id"))) class Movie(Base): __tablename__ = "movies" id = Column(Integer, primary_key=True) title = Column

flask-admin form: Constrain Value of Field 2 depending on Value of Field 1

吃可爱长大的小学妹 提交于 2019-12-17 18:40:17
问题 One feature I have been struggling to implement in flask-admin is when the user edits a form, to constrain the value of Field 2 once Field 1 has been set. Let me give a simplified example in words (the actual use case is more convoluted). Then I will show a full gist that implements that example, minus the "constrain" feature. Let's say we have a database that tracks some software "recipes" to output reports in various formats. The recipe table of our sample database has two recipes: "Serious

Flask-SQLAlchemy check if row exists in table

◇◆丶佛笑我妖孽 提交于 2019-12-17 15:43:37
问题 I have a Flask application which uses Flask-SQLAlchemy to connect to a MySQL database. I would like to be able to check whether a row is present in a table. How would I modify a query like so to check the row exists: db.session.query(User).filter_by(name='John Smith') I found a solution on this question which uses SQLAlchemy but does not seem to fit with the way Flask-SQLAlchemy works: from sqlalchemy.sql import exists print session.query(exists().where(User.email == '...')).scalar() Thanks.

AttributeError: 'int' object has no attribute '_sa_instance_state'

守給你的承諾、 提交于 2019-12-17 09:33:10
问题 I'm working on forum template using Flask. When I attempt creating a new thread in the browser using forms, SQLAlchemy throws an AttributeError. The problem showed up when I tried implementing a one-to-many relationship with Forum-to-Thread and a one-to-many relationship with Thread-to-User. models.py class User(db.Model): id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(32), index=True, unique=True) password = db.Column(db.String(32), index=True) email = db.Column

AttributeError: 'int' object has no attribute '_sa_instance_state'

最后都变了- 提交于 2019-12-17 09:32:16
问题 I'm working on forum template using Flask. When I attempt creating a new thread in the browser using forms, SQLAlchemy throws an AttributeError. The problem showed up when I tried implementing a one-to-many relationship with Forum-to-Thread and a one-to-many relationship with Thread-to-User. models.py class User(db.Model): id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(32), index=True, unique=True) password = db.Column(db.String(32), index=True) email = db.Column

Get inserted key before commit session

≡放荡痞女 提交于 2019-12-17 07:24:28
问题 class Parent(db.Model): id = db.Column(db.Integer, primary_key=True) class Child(db.Model): id = db.Column(db.Integer, primary_key=True) parent_id = db.Column(db.Integer, db.ForeignKey('parent.id')) parent = Parent() db.session.add(parent) child = Child() child.parent_id = parent.id db.session.add(child) db.session.commit() I want to INSERT into both parent and child tables inside a session considering that the parent_id must be included in the child table. In the moment I create the child

Tyring to set up modelview with Flask-Admin causes ImportError

拥有回忆 提交于 2019-12-17 06:18:05
问题 I am trying to add a User model view to Flask-Admin. However, I get ImportError: cannot import name db . Why is this happening and how do I fix it? app/__init__.py : from flask import Flask from flask_sqlalchemy import SQLAlchemy import flask_admin as admin from app.models import User db = SQLAlchemy() admin = admin.Admin(name="Admin Panel") def create_app(config_name): app = Flask(__name__) db.init_app(app) admin.init_app(app) admin.add_view(ModelView(User, db.session)) return app app/models

ImportError: No module named flaskext.sqlalchemy

寵の児 提交于 2019-12-17 05:15:15
问题 I am trying to use the Sign in with SteamID snippet from the Flask site. However, I get ImportError: No module named flaskext.sqlalchemy when I try to run it, and PyCharm says Uresolved reference "flaskext" and Uresolved reference "OpenID" . I re-installed Flask-OpenID and Flask-SQLAlchemy to make sure they were there. Why am I getting this error and how do I fix it? 回答1: That snippet is really old. flaskext is no more (or at least very deprecated). Refer to packages directly rather than

ImportError: No module named flaskext.sqlalchemy

佐手、 提交于 2019-12-17 05:15:12
问题 I am trying to use the Sign in with SteamID snippet from the Flask site. However, I get ImportError: No module named flaskext.sqlalchemy when I try to run it, and PyCharm says Uresolved reference "flaskext" and Uresolved reference "OpenID" . I re-installed Flask-OpenID and Flask-SQLAlchemy to make sure they were there. Why am I getting this error and how do I fix it? 回答1: That snippet is really old. flaskext is no more (or at least very deprecated). Refer to packages directly rather than

How to build a flask application around an already existing database?

好久不见. 提交于 2019-12-17 04:10:49
问题 I already have an existing Database that has a lot of tables and a lot of data in MySQL . I intend to create a Flask app and use sqlalchemy along with it. Now I asked out on irc and looked around on google and tried the following ideas: First I used sqlacodegen to generate the models from my DB . But then I was confused about it a little and looked some more. And I found this. This looked like an elegant solution. So Second , I rewrote my models.py according to the solution there and now I am