flask-sqlalchemy

Render Jinja after jQuery AJAX request to Flask

邮差的信 提交于 2019-12-03 07:58:38
问题 I have a web application that gets dynamic data from Flask when a select element from HTML is changed. of course that is done via jquery ajax . No probs here I got that. The problem is, the dynamic data - that is sent by Flask - , is a list of objects from the database Flask-sqlalchemy . Of course the data is sent as JSON from Flask . I'd like to iterate through those objects to display their info using Jinja . HTML <select id="#mySelect"> <option value="option1" id="1">Option 1 </option>

Configuring Flask-SQLAlchemy to use multiple databases with Flask-Restless

倖福魔咒の 提交于 2019-12-03 07:24:52
问题 I have a Flask app that uses Flask-SQLAlchemy and I'm trying to configure it to use multiple databases with the Flask-Restless package. According to the docs, configuring your models to use multiple databases with __bind_key__ seems pretty straightforward. However it doesn't seem to be working for me. I create my app and initialise my database like this: from flask import Flask from flask.ext.sqlalchemy import SQLAlchemy SQLALCHEMY_DATABASE_URI = 'postgres://db_user:db_pw@localhost:5432/db

How Can I Automatically Populate SQLAlchemy Database Fields? (Flask-SQLAlchemy)

狂风中的少年 提交于 2019-12-03 06:36:52
问题 I've got a simple User model, defined like so: # models.py from datetime import datetime from myapp import db class User(db.Model): id = db.Column(db.Integer(), primary_key=True) email = db.Column(db.String(100), unique=True) password = db.Column(db.String(100)) date_updated = db.Column(db.DateTime()) def __init__(self, email, password, date_updated=None): self.email = email self.password = password self.date_updated = datetime.utcnow() When I create a new User object, my date_updated field

Python Flask-WTF - use same form template for add and edit operations

半腔热情 提交于 2019-12-03 05:59:40
问题 I'm just getting started with Flask / Flask-WTF / SQLAlchemy, and most example CRUD code I see shows separate templates for adding / editing. It seems repetitive to have two templates with almost identical form html (e.g. books_add.html, books_edit.html). Conceptually it makes more sense to me to have one template, something like "books_form.html", and just call render_template on that same template from two separate route definitions. I'm not quite sure how to accomplish it though, something

SQLAlchemy and Falcon - session initialization

帅比萌擦擦* 提交于 2019-12-03 05:42:23
I'm wondering where the best place would be to create a scoped session for use in falcon. From reading the flask-sqlalchemy code, it, in a round about way, does something like this: from sqlalchemy import create_engine from sqlalchemy.orm import scoped_session, sessionmaker try: from greenlet import get_current as get_ident except ImportError: try: from thread import get_ident except ImportError: from _thread import get_ident connection_uri = 'postgresql://postgres:@localhost:5432/db' engine = create_engine(connection_uri) session_factory = sessionmaker(bind=engine) session_cls = scoped

scoped_session(sessionmaker()) or plain sessionmaker() in sqlalchemy?

陌路散爱 提交于 2019-12-03 05:36:34
问题 I am using SQlAlchemy in my web project. What should I use - scoped_session(sessionmaker()) or plain sessionmaker() - and why? Or should I use something else? ## model.py from sqlalchemy import * from sqlalchemy.orm import * engine = create_engine('mysql://dbUser:dbPassword@dbServer:dbPort/dbName', pool_recycle=3600, echo=False) metadata = MetaData(engine) Session = scoped_session(sessionmaker()) Session.configure(bind=engine) user = Table('user', metadata, autoload=True) class User(object):

Defining SQLAlchemy enum column with Python enum raises “ValueError: not a valid enum”

心已入冬 提交于 2019-12-03 05:34:59
问题 I am trying to follow this example to have an enum column in a table that uses Python's Enum type. I define the enum then pass it to the column as shown in the example, but I get ValueError: <enum 'FruitType'> is not a valid Enum . How do I correctly define a SQLAlchemy enum column with a Python enum? from flask import Flask from flask_sqlalchemy import SQLAlchemy import enum app = Flask(__name__) db = SQLAlchemy(app) class FruitType(enum.Enum): APPLE = "Crunchy apple" BANANA = "Sweet banana"

Flask-SQLAlchemy: How to conditionally insert or update a row

感情迁移 提交于 2019-12-03 04:22:43
问题 My application uses a combination of Flask, Flask-SQLAlchemy, Flask-WTF and Jinja2. In its current incarnation, I have a settings table. The table will only have one record with one field. Initially the table contains zero records. What I want to achieve is: Given that no entries exist in db, then show empty form ready for user input Given that an entry exist, show the entry, and if the user changes the value, then update the rec in db. Here is my code: models.py class Provider(db.Model): id

Problems trying to mock a Model within Flask-SQLAlchemy

眉间皱痕 提交于 2019-12-03 03:19:50
I'm testing a Flask application that have some SQLAlchemy models using Flask-SQLAlchemy and I'm having some problems trying to mock a few models to some methods that receive some models as parameters. A toy version of what I'm trying to do is like this. Suppose I have a model given by: // file: database.py from flask_sqlalchemy import SQLAlchemy db = SQLAlchemy() class User(db.Model): id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(80), unique=True) birthday = db.Column(db.Date) That is imported in an app that is built with the app factory pattern: // file: app.py

Flask-SQLAlchemy Query Join relational tables

≯℡__Kan透↙ 提交于 2019-12-03 02:52:14
问题 I am building an app using Flask & SQLAlchemy. I basically have 3 tables: users, friendships and bestFriends: A user can have many friends but only one best friend. So I want my model to be relational. 'One-to-many' for the relation between 'users' and 'friendships' & 'one-to-one' between 'users' and 'bestFriends'. This is my Model: from app import db from sqlalchemy.orm import relationship, backref from sqlalchemy import Table, Column, Integer, ForeignKey from sqlalchemy.ext.declarative