flask-sqlalchemy

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

流过昼夜 提交于 2019-11-28 07:31:45
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): __tablename__ = 'users' id = db.Column(db.Integer, primary_key = True) email = db.Column(db.String

SQLAlchemy delete doesn't cascade

非 Y 不嫁゛ 提交于 2019-11-28 06:23:30
My User model has a relationship to the Address model. I've specified that the relationship should cascade the delete operation. However, when I query and delete a user, I get an error that the address row is still referenced. How do I delete the user and the addresses? class User(db.Model): id = db.Column(db.Integer, primary_key=True) addresses = db.relationship('Address', cascade='all,delete', backref='user') class Address(db.Model): id = db.Column(db.Integer, primary_key=True) user_id = db.Column(db.Integer, db.ForeignKey(User.id)) db.session.query(User).filter(User.my_id==1).delete()

Can I avoid circular imports in Flask and SQLAlchemy

ⅰ亾dé卋堺 提交于 2019-11-28 05:59:28
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 for db with SQLAlchemy (or can it?). Take a look at this project: https://github.com/sloria/cookiecutter

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

一世执手 提交于 2019-11-28 05:27:33
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 request.method == 'POST': form = EditProfile() form_action = url_for('profile.editprofile') if request

Flask-SQLAlchemy Constructor

让人想犯罪 __ 提交于 2019-11-28 04:23:52
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 = email for a table with two columns, that might be acceptable, but what if I have tables with 10+ columns?

SQLAlchemy sessions error

折月煮酒 提交于 2019-11-28 03:54:26
问题 Background: Flask / Flask-SQLAlchemy / Flask-WTF, using declarative and scoped session Simple POST operation: @tas.route('/order_add', methods=['GET', 'POST']) def tas_order_add(): if request.method == 'POST': order_form = OrderForm() if order_form.validate_on_submit(): order = Order() order_form.populate_obj(order) db_session.add(order) db_session.commit() Now trying to run it I get an error: InvalidRequestError: Object '' is already attached to session '1' (this is '2') Changing add to

How to delete a record by id in Flask-SQLAlchemy

本小妞迷上赌 提交于 2019-11-28 03:52:47
I have users table in my MySql database. This table has id , name and age fields. How can I delete some record by id ? Now I use the following code: user = User.query.get(id) db.session.delete(user) db.session.commit() But I don't want to make any query before delete operation. Is there any way to do this? I know, I can use db.engine.execute("delete from users where id=...") , but I would like to use delete() method. Thanks! adarsh You can do this, User.query.filter_by(id=123).delete() or User.query.filter(User.id == 123).delete() Make sure to commit for delete() to take effect. Just want to

Switching from SQLite to MySQL with Flask SQLAlchemy

一世执手 提交于 2019-11-28 03:09:06
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 my MySQL database tool to an appropriately formatted URL. But I'm worried about (2), I had assumed that

Readonly text field in Flask-Admin ModelView

风流意气都作罢 提交于 2019-11-28 02:58:12
问题 How can I make a field on a ModelView readonly? class MyModelView(BaseModelView): column_list = ('name', 'last_name', 'email') 回答1: If you're talking about Flask-Admin with SQLAlchemy Models, and you're declaring a view by inheriting from sqlamodel.ModelView, you can just add this to your class definition: class MyModelView(BaseModelView): column_list = ('name', 'last_name', 'email') form_widget_args = { 'email':{ 'disabled':True } } 回答2: I don't have enough reputation to comment on @thkang's

How to use Flask-SQLAlchemy in a Celery task

為{幸葍}努か 提交于 2019-11-28 02:49:14
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 have full access to SQLAlchemy. I am trying to replicate this behavior in my tasks.py file but with no