flask-sqlalchemy

Modeling relationships in SQLAlchemy to show nested “sets” with Marshmallow

依然范特西╮ 提交于 2020-02-07 05:30:27
问题 I'm trying to figure out how to model (flask-)SQLAlchemy and (flask-)Marshamallow to provide the following JSON output. The parent is a product and the children are "variant sets"... which are types of options, like "color" or "size". Nested within those sets I want the options themselves (S, M, L, etc.) Seems like I'm missing something obvious. Desired output: { "skuid": "B1234", "name": "Test Product 1", "variant_sets": [ { "variant_set": "B1234_1", "variants": [ { "code": "S", "variant

SQLAlchemy ORM to load specific columns in Model.query

强颜欢笑 提交于 2020-02-06 07:56:53
问题 I am newbie in python/sqlachemy world. I am trying to fetch data from PostgreSQL using sqlachemy in flask, where I need to fetch only selected column instead of streaming all the column from database through network. One of the approach is using session (like below) which is working fine, session.query(User.user_id, User.name).all() But for some reason I would like to stick with Model.query method instead of using sessions. So I ended up to use something like below, User.query.options(load

delete one-to-one relationship in flask

冷暖自知 提交于 2020-02-06 02:54:16
问题 I'm currently developing an application using flask and I'm having a big problem to delete items in an one-to-one relationship. I have the following structure in my models: class User(db.Model): __tablename__ = 'user' user_id = db.Column(db.String(8), primary_key = True) password = db.Column(db.String(26)) class Student(db.Model): __tablename__ = 'student' user_id = Column(db.String(8), ForeignKey('user.user_id'), primary_key = True) user = db.relationship('User') class Professor(db.Model): _

flask_sqlalchemy create_all without having to import models

為{幸葍}努か 提交于 2020-02-04 03:47:31
问题 I am trying to understand how to set up a standalone script that calls create_all without having to import all my models into it. Below are the relevant files: db.py from flask_sqlalchemy import SQLAlchemy db = SQLAlchemy() test_model.py from db import db class TestTable(db.model): id = db.Column(db.Integer, primary_key=True) foo = db.Column(db.String(80)) and create_all.py from db import db db.create_all() However, create_all will not do anything, as it does not detect any tables registered

flask_sqlalchemy create_all without having to import models

允我心安 提交于 2020-02-04 03:47:29
问题 I am trying to understand how to set up a standalone script that calls create_all without having to import all my models into it. Below are the relevant files: db.py from flask_sqlalchemy import SQLAlchemy db = SQLAlchemy() test_model.py from db import db class TestTable(db.model): id = db.Column(db.Integer, primary_key=True) foo = db.Column(db.String(80)) and create_all.py from db import db db.create_all() However, create_all will not do anything, as it does not detect any tables registered

flask-sqlalchemy with_entities and relationships

偶尔善良 提交于 2020-02-03 18:53:49
问题 I want to select only a couple columns from my model to speed up the queries, but one of the columns I want to select is from a relationship. Models: class OpenChromatinRegion(db.Model): ... gene_id = db.Column(db.Integer, db.ForeignKey("gene.id"), nullable=False, index=True) gene = db.relationship("Gene", back_populates='open_chromatin_regions') class Gene(db.Model): id = db.Column(db.Integer, primary_key=True) ENSEMBLID = db.Column(db.Integer, index=True, unique=True, nullable=False) ...

Share sqlalchemy models between flask and other apps

天大地大妈咪最大 提交于 2020-01-28 03:15:19
问题 I have a running Flask application that is set up according to a combination of best practices we found online and in Miguel Grinberg's "Flask Web Development" book. We now need a second Python application, that is NOT a web app, and that needs access to the same models as the Flask application. We wanted to re-use the same models ofcourse, so both apps can benefit from the shared code. We have removed dependencies on the flask-sqlalchemy extention (which we used before, when we had just the

flask request url not found on the server “Error: 404 not found”?

风格不统一 提交于 2020-01-25 06:41:22
问题 I'm working on flask application. The route localhost:5000/user/login is not found. All other routes are working perfectly but I don't know what's going wrong with this route! Would you help me to fix this error? This route localhost:5000/user/login is available but It's saying 404 not found . run.py from app import app print(app.url_map) if __name__ == '__main__': app.run() bytewar / init.py from flask import Flask, url_for, redirect app = Flask(__name__, static_folder='static') app.config

Flask SQLAlchemy import DB import

南笙酒味 提交于 2020-01-24 21:19:09
问题 I am using flask with SQLAlchemy, after set all configuration setting I got db import error on model. ImportError: cannot import name 'db' my main app __init__ from flask_api import FlaskAPI from flask_sqlalchemy import SQLAlchemy import os import json from flask import Flask from flask_pymongo import PyMongo from flask import request from app.test.controllers import test def create_app(config_name): app = FlaskAPI(__name__) CORS(app) app.config.from_object(os.environ['APP_SETTINGS']) db =

flask wtforms-alchemy QuerySelectField ValueError: too many values to unpack (expected 2)

旧街凉风 提交于 2020-01-24 20:01:09
问题 i am trying to create sign up form using flask-wtf , and wtforms-alchemy on this form i try create selectfield, which the value is query from my models. but i'am always get this error: ValueError: too many values to unpack (expected 2) this my code : from flask_wtf import FlaskForm from wtforms_sqlalchemy.fields import QuerySelectField def choose_domicile(): return Domicile.query class RegisterForm(FlaskForm): name = StringField('Name', validators=[DataRequired()]) domicile = QuerySelectField