flask-sqlalchemy

Flask-SQLAlchemy Constructor

放肆的年华 提交于 2019-11-27 05:19:39
问题 in the Flask-SQLAlchemy tutorial, a constructor for the User model is defined: from flask import Flask from flask.ext.sqlalchemy import SQLAlchemy app = Flask(__name__) app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:////tmp/test.db' db = SQLAlchemy(app) class User(db.Model): id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(80), unique=True) email = db.Column(db.String(120), unique=True) def __init__(self, username, email): self.username = username self.email =

Flask-Login - How to get Session ID

岁酱吖の 提交于 2019-11-27 02:18:53
问题 Am doing a project with Flask, Gevent and web socket using flask development server environment. I used flask_login . Here how can get i get the Unique Session ID for each connection? I want to store the SessionID in the Database and delete it once client disconnects. How to get total active connections from flask_login import * login_manager = LoginManager() login_manager.setup_app(app) @app.route("/", methods=["GET", "POST"]) def login(): login_user([username], remember): @app.route("

How do I declare a base model class in Flask-SQLAlchemy?

点点圈 提交于 2019-11-27 01:49:50
问题 I would like to declare a base class that all other schema objects can inherit from, for example: class Base(db.Model): created_on = db.Column(db.DateTime, default=db.func.now()) updated_on = db.Column(db.DateTime, default=db.func.now(), onupdate=db.func.now()) Then all other schema objects can inherit from it and not have to repeat the declaration of the two columns. How would I do this in Flask-SQLAlchemy? from flask.ext.sqlalchemy import SQLAlchemy db = SQLAlchemy(app) class User(db.Model)

SQLAlchemy - performing a bulk upsert (if exists, update, else insert) in postgresql

不问归期 提交于 2019-11-27 01:03:13
问题 I am trying to write a bulk upsert in python using the SQLAlchemy module (not in SQL!). I am getting the following error on a SQLAlchemy add: sqlalchemy.exc.IntegrityError: (IntegrityError) duplicate key value violates unique constraint "posts_pkey" DETAIL: Key (id)=(TEST1234) already exists. I have a table called posts with a primary key on the id column. In this example, I already have a row in the db with id=TEST1234 . When I attempt to db.session.add() a new posts object with the id set

Pre-Populate a WTforms in flask, with data from a SQLAlchemy object

馋奶兔 提交于 2019-11-27 00:56:57
问题 I am fairly new to flask framework and was creating an edit profile page for a webportal. I am stuck at a point and am unable to autofill a form. Here is my form class : class EditProfile(Form): username = TextField('Username', [Required()]) email = TextField('Email', [Required()]) about = TextAreaField('About', [Required()]) website = TextField('Website', [Required()]) This is my function that evaluates the form. def editprofile(nickname = None): if g.fas_user['username'] == nickname or

Can I avoid circular imports in Flask and SQLAlchemy

时光毁灭记忆、已成空白 提交于 2019-11-27 00:56:42
问题 app/ init .py: from flask import Flask from flask_sqlalchemy import SQLAlchemy app = Flask(__ name __) db = SQLAlchemy(app) from app import views, models app/models.py: from app import db # I want to avoid this everywhere I really don't like my submodules having a dependency on their parent. Also can the global package variables be avoided too? I want a more OO solution. One alternative for app is to use Blueprints I think, but then I loose the route decorator. Also the same cannot be done

Circular import of db reference using Flask-SQLAlchemy and Blueprints

时光毁灭记忆、已成空白 提交于 2019-11-27 00:42:48
问题 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):

Setting delete-orphan on SQLAlchemy relationship causes AssertionError: This AttributeImpl is not configured to track parents

梦想与她 提交于 2019-11-27 00:28:38
问题 this is my Flask-SQLAlchemy Declarative code: from sqlalchemy.ext.associationproxy import association_proxy from my_flask_project import db tagging = db.Table('tagging', db.Column('tag_id', db.Integer, db.ForeignKey('tag.id', ondelete='cascade'), primary_key=True), db.Column('role_id', db.Integer, db.ForeignKey('role.id', ondelete='cascade'), primary_key=True) ) class Tag(db.Model): id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(100), unique=True, nullable=False) def

Switching from SQLite to MySQL with Flask SQLAlchemy

大憨熊 提交于 2019-11-26 23:57:39
问题 I have a site that I've built with Flask SQLAlchemy and SQLite, and need to switch to MySQL. I have migrated the database itself and have it running under MySQL, but Can't figure out how to connect to the MySQL database (that is, what the SQLALCHEMY_DATABASE_URI should be) and Am unclear if any of my existing SQLAlchemy SQLite code will work with MySQL. I suspect that (1) is fairly simple and just a matter of being shown how to map, for example, the contents of the connection dialog I use in

How to use Flask-SQLAlchemy in a Celery task

旧城冷巷雨未停 提交于 2019-11-26 23:49:45
问题 I recently switch to Celery 3.0. Before that I was using Flask-Celery in order to integrate Celery with Flask. Although it had many issues like hiding some powerful Celery functionalities but it allowed me to use the full context of Flask app and especially Flask-SQLAlchemy. In my background tasks I am processing data and the SQLAlchemy ORM to store the data. The maintainer of Flask-Celery has dropped support of the plugin. The plugin was pickling the Flask instance in the task so I could