flask-sqlalchemy

Creating one-to-one relationship Flask-SQLAlchemy

泪湿孤枕 提交于 2020-01-24 12:57:45
问题 I am trying to create a one-to-one relationship between a Department & Ticket table. This way when looking inside of Flask-Admin a user would be able to see the Department name instead of the ID. I have tried to setup the relationship as follows: # Ticket Table class Tickets(db.Model): __tablename__ = 'tickets' ticketID = db.Column(db.Integer, nullable=False, primary_key=True, autoincrement=True, unique=True) cust_name = db.Column(db.String(50), nullable=False) cust_email = db.Column(db

reading from joined query in flask-sqlalchemy

扶醉桌前 提交于 2020-01-24 12:43:06
问题 After successfully joining two db tables, I'm trying to read the data from the 2nd table by addressing the first. I'm addressing opinion.topic_name from my jinja2 template, but nothing is returned. How can I access the Topic.topic_name value from the joined query? view @main.route('/', methods=['GET', 'POST']) def index(): form = IndexForm() opinions = [] if form.validate_on_submit(): opinions = Opinion.query .filter_by(party_id=form.party.data) .filter_by(topic_id=form.topic.data) .join(

Do I need to create a sessions table to use Flask-Session SqlAlchemySessionInterface

 ̄綄美尐妖づ 提交于 2020-01-24 12:37:27
问题 I am attempting to implement Flask-Session in my python application. I read in the docs that its recommended to use another interface like the SqlAlchemySessionInterface instead of the default NullSessionInterface which is used when nothing is provided to the SESSION_TYPE configuration key. From the flask_session/ init .py file under class Session it reads By default Flask-Session will use :class: NullSessionInterface , you really should configurate your app to use a different

Display first element of a one-to-many relationship in Flask-Admin

淺唱寂寞╮ 提交于 2020-01-23 16:13:05
问题 I'm currently using Flask-Admin to create an admin screen but I'm running into a roadblock. Suppose I have two classes that look like this: class Part(db.Model): id = db.Column(db.Integer, primary_key=True, unique=True) part_data = db.relationship("PartHistory", backref="part") name = db.Column(db.String(255)) class PartHistory(db.Model): id = db.Column(db.Integer, primary_key=True, unique=True) part_id = db.Column(db.Integer, db.ForeignKey('part.id')) date_checked = db.Column(db.Date) Part

Display first element of a one-to-many relationship in Flask-Admin

好久不见. 提交于 2020-01-23 16:12:27
问题 I'm currently using Flask-Admin to create an admin screen but I'm running into a roadblock. Suppose I have two classes that look like this: class Part(db.Model): id = db.Column(db.Integer, primary_key=True, unique=True) part_data = db.relationship("PartHistory", backref="part") name = db.Column(db.String(255)) class PartHistory(db.Model): id = db.Column(db.Integer, primary_key=True, unique=True) part_id = db.Column(db.Integer, db.ForeignKey('part.id')) date_checked = db.Column(db.Date) Part

Point type in sqlalchemy?

谁说我不能喝 提交于 2020-01-23 06:06:35
问题 I found this regarding Point type in Postgres: http://www.postgresql.org/docs/current/interactive/datatype-geometric.html Is there the SQLAlchemy version of this? I am storing values in this manner: (40.721959482, -73.878993913) 回答1: You can use geoalchemy2 whis is an extension to sqlalchemy and can be used with flask-sqlalchemy too. from sqlalchemy import Column from geoalchemy2 import Geometry # and import others class Shop(db.Model): # other fields coordinates = Column(Geometry('POINT'))

How to do a sqlalchemy query using a string variable as table name?

橙三吉。 提交于 2020-01-23 03:44:48
问题 I apologize in advance if I am not clear, English is not my native language. Feel free to tell me if I make too many mistakes :) I am a newbie in using flask_sqlalchemy and get frustrated after spending hours on Internet searching for an answer. What I want is doing a query like that one : ModelName.query.filter_by(name='something').all() But instead of using ModelName I want to do something like that : model_list = ['Model1', 'Model2', 'Model3'] for model in model_list: some_var = model

sqlalchemy session not getting removed properly in flask testing

|▌冷眼眸甩不掉的悲伤 提交于 2020-01-22 14:49:49
问题 I'm using Flask-Testing which says: Another gotcha is that Flask-SQLAlchemy also removes the session instance at the end of every request (as should any threadsafe application using SQLAlchemy with scoped_session). Therefore the session is cleared along with any objects added to it every time you call client.get() or another client method. However, I'm not seeing that. This test fails: from flask import Flask from flask.ext.sqlalchemy import SQLAlchemy from flask.ext.testing import TestCase

Flask-SQLAlchemy - model has no attribute 'foreign_keys'

冷暖自知 提交于 2020-01-22 13:26:32
问题 I have 3 models created with Flask-SQLalchemy: User, Role, UserRole user.py: class Role( ActiveRecord, db.Model ): __tablename__ = "roles" # Schema id = db.Column( db.Integer, primary_key = True ) name = db.Column( db.String( 24 ), unique = True ) description = db.Column( db.String( 90 ) ) users = db.relationship( "User", secondary = "UserRole", \ backref = db.backref( "roles" ) ) role.py: class User( db.Model, ActiveRecord ): __tablename__ = "users" # Schema id = db.Column( db.Integer,

Unable to create self referencing foreign key in flask-sqlalchemy

白昼怎懂夜的黑 提交于 2020-01-22 09:55:10
问题 I have a model Region and each Region can have sub-regions. Each sub-region has a field parent_id which is the id of its parent region. Here is how my model looks like class Region(db.Model): __tablename__ = 'regions' __table_args__ = {'schema': 'schema_name'} id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(100)) parent_id = db.Column(db.Integer, db.ForeignKey('regions.id')) parent = db.relationship('Region', primaryjoin=('Region.parent_id==Region.id'), backref='sub