flask-sqlalchemy

Setting a default role in flask-security

假如想象 提交于 2019-12-03 21:13:29
I am trying to set a default role when a user registers with my site, currently no roles are set when the user registers. I have created the roles I need, so I just need to define it somehow. Not sure how though. The code I have is pretty much copy paste from the quick start guide. Anyway, here it is: # Define models roles_users = db.Table('roles_users', db.Column('user_id', db.Integer(), db.ForeignKey('user.id')), db.Column('role_id', db.Integer(), db.ForeignKey('role.id'))) class Role(db.Model, RoleMixin): id = db.Column(db.Integer(), primary_key=True) name = db.Column(db.String(80), unique

Structure Flask-Restful API to use SQLAlchemy

回眸只為那壹抹淺笑 提交于 2019-12-03 20:25:21
问题 So I'm trying to make an API, using Flask-Restful, but all the examples I find put everything into one app.py file. I found information in the Flask-Restful docs explaining how to structure your API, but it doesn't include anything for using a database. I've posted what I've come up with, and it works if I hard-code some data, but when I import the db into users.py I get an error ImportError: cannot import name 'db' . So, what is the best way to structure an API to bring in your data from a

Using Flask-SQLAlchemy in Blueprint models without reference to the app [closed]

六月ゝ 毕业季﹏ 提交于 2019-12-03 18:22:15
问题 Closed . This question is opinion-based. It is not currently accepting answers. Want to improve this question? Update the question so it can be answered with facts and citations by editing this post. Closed last year . I'm trying to create a "modular application" in Flask using Blueprints. When creating models, however, I'm running into the problem of having to reference the app in order to get the db -object provided by Flask-SQLAlchemy. I'd like to be able to use some blueprints with more

Self-Referential Association Relationship SQLalchemy

老子叫甜甜 提交于 2019-12-03 17:19:09
In my flask application with flask-sqlalchemy i need to create association between two contact here is my Contact model class Contact(db.Model): __tablename__ = 'contact' id = db.Column(db.Integer, primary_key=True) name = db.Column(db.Unicode(120), nullable=False, unique=False) user_id = db.Column(db.Integer, db.ForeignKey('user.id')) to_contacts = db.relationship('Contact', secondary='ContactRelation', primaryjoin='id==contactrelation.c.from_contact_id', secondaryjoin='id==contactrelation.c.to_contact_id', backref='from_contacts') and my association class ContactRelation : class

SQLAlchemy and Falcon - session initialization

时光总嘲笑我的痴心妄想 提交于 2019-12-03 17:15:10
问题 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

Flask and SQLAlchemy and the MetaData object

[亡魂溺海] 提交于 2019-12-03 17:07:47
it's the first time i am using this environment. The part of SQLAlchemy i am willing to use is just the one that allows me to query the database using Table objects with autoload = True. I am doing this as my tables already exist in the DB (mysql server) and were not created by defining flask models. I have gone through all the documentation and i don't seem to find an answer. Here is some code: app = Flask(__name__) app.config.from_object(__name__) metadata = None def connect_db(): engine = create_engine(app.config['DATABASE_URI']) global metadata metadata = MetaData(bind=engine) return

Flask-SQLAlchemy and Flask-Restless not fetching grandchildren

淺唱寂寞╮ 提交于 2019-12-03 16:13:22
Problem I am building an app on Flask, Flask-SQLAlchemy, and Flask-Restless. I have used restless to generate an API for a parent-child-grandchild relationship*. A GET on my child will correctly fetch the grandchild, but a GET on the parent will not fetch the grandchild for each child. *In fact, the parent-child relationship is a many-to-many, but same premise. Models class Grandchild(db.Model): id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String, nullable=False) parent = db.relationship('Child', backref='grandchild') parent_child = db.Table('parent_child', db.Column(

db.Model vs db.Table in Flask-SQLAlchemy [closed]

谁说胖子不能爱 提交于 2019-12-03 16:11:34
The Flask-SQLAlchemy docs say that many-to-many lookup tables should not subclass db.Model but instead be written as db.Tables. From the docs: If you want to use many-to-many relationships you will need to define a helper table that is used for the relationship. For this helper table it is strongly recommended to not use a model but an actual table Why? What are the downsides to making everything a model? I think it looks cleaner to have a unified way of declaring tables in the database. Also, it's possible that sometime later a developer will want to access those mapping records directly,

How to paginate in Flask-SQLAlchemy for db.session joined queries?

爷,独闯天下 提交于 2019-12-03 14:08:35
Say, we have the following relationships: a person can have many email addresses a email service provider can (obviously) serve multiple email address So, it's a many to many relationship. I have three tables: emails, providers, and users. Emails have two foreign ids for provider and user. Now, given a specific person, I want to print all the email providers and the email address it hosts for this person, if it exists. (If the person do not have an email at Gmail, I still want Gmail be in the result. I believe otherwise I only need a left inner join to solve this.) I figured out how to do this

Problems trying to mock a Model within Flask-SQLAlchemy

不羁的心 提交于 2019-12-03 13:04:11
问题 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