flask-sqlalchemy

Search on non-text column in flask-admin

橙三吉。 提交于 2019-12-11 12:12:41
问题 I have a sqlalchemy model: class Multicast(db.Model): __tablename__ = "multicast" id = Column(Integer, primary_key=True) addr = Column(Inet) name = Column(Unicode(65)) I need to make search by "addr" field. I could not do it in this way: class MulticastView(ModelView): column_searchable_list = ('name', 'addr',) column_filters = ('name', ) model = Multicast Because i had an Exception: Can only search on text columns. Failed to setup search for "addr". How can i make this search? 回答1: I found

SqlAlchemy (Postgres + Flask ) : How to sum multiple columns?

戏子无情 提交于 2019-12-11 12:10:42
问题 I have a class Score with a column item_id and several fields having different scores types(score1, score2, score3...)all having integer values. I need to write a query that takes the list of scores types and returns a list with objects having itemid and sum of the scores of all score types mentioned in the list alongside. I'm trying to do this using hybrid method but confused about how to write the query. model.py class Score(db.Model): __tablename__ = 'scores' item_id = db.Column(db.Integer

'Engine' object has no attribute 'drivername'

爷,独闯天下 提交于 2019-12-11 11:29:01
问题 Im integrate Flask and SQLAlchemy in my application, but can understand, why at login page in getting big stacktrace. This errors now I'll taking after switching to Mac OS from Debian. All used libraries in virtuaelnv. I have right now these things: 1) in flask_app.py: from flask import Flask, request, session from flask_sqlalchemy import SQLAlchemy from db import tables as dbTables from db.queries import engine as db_engine app = Flask(__name__, template_folder='./web/templates/', static

Construct a query to filter many-to-many for all B instances related to a given A instance

≯℡__Kan透↙ 提交于 2019-12-11 10:29:44
问题 I want to get all TableB instances that are related to a specific TableA instance. It is a many-to-many relationship. How do I construct this query? class TableA(db.Model): __tablename__ = 'tableA' id = db.Column(db.Integer, primary_key=True) class TableB(db.Model): __tablename__ = 'tableB' id = db.Column(db.Integer, primary_key=True) many_to_many = db.relationship(TableA, secondary='association_table') association_table = db.Table('association', db.Column('tableA_id', db.Integer, db

MySQL Connection not available when use SQLAlchemy(MySQL) and Flask

99封情书 提交于 2019-12-11 09:26:28
问题 I'm getting this error sometime (sometime is ok, sometime is wrong): sqlalchemy.exc.OperationalError: (OperationalError) MySQL Connection not available. while using session.query I'm writing a simple server with Flask and SQLAlchemy (MySQL). My app.py like this: Session = sessionmaker(bind=engine) session = Session() @app.route('/foo') def foo(): try: session.query(Foo).all() except Exception: session.rollback() Update I also create new session in another file and call it in app.py Session =

SQLAlchemy Reflection Using Metaclass with Column Override

好久不见. 提交于 2019-12-11 09:18:14
问题 I have a set of dynamic database tables (Postgres 9.3 with PostGIS) that I am mapping using a python metaclass: cls = type(str(tablename), (db.Model,), {'__tablename__':tablename}) where, db.Model is the db object via flask-sqlalchemy and tablename is a bit of unicode. The cls is then added to an application wide dictionary current_app.class_references (using Flask's current_app) to avoid attempts to instantiate the class multiple times. Each table contains a geometry column, wkb_geometry

Update model with WTForms form data

本秂侑毒 提交于 2019-12-11 08:27:17
问题 I have some Flask-SQLAlchemy models and Flask-WTF forms generated with wtforms_alchemy to represent them. I implemented a method on each model to update its attributes from a form's data. For each new model and field I have to update these methods, which is annoying. Is there a way to make this more automatic, or a a feature in the libraries I'm using that I'm missing? def edit_car(car_id): form = CarForm(request.form) if form.is_valid(): car = Car.query.get_or_404(car_id) car.from_form(form)

order by a method contained within a class in SQLAlchemy

喜你入骨 提交于 2019-12-11 07:57:27
问题 I am currently working on a model where I will judge the relevance of an article. This follows Hacker News' algorithm. Here is my articles model in app/articles/models.py from app.extensions import db class Article(db.Model): """ database representation of an article """ id = db.Column(db.Integer, primary_key=True) title = db.Column(db.String(128)) subtitle = db.Column(db.String(512)) body = db.Column(db.Text()) votes = db.Column(db.Integer, default=1) views = db.Column(db.Integer, default=1)

More specific SQL query with flask-wtf queryselectfield

天涯浪子 提交于 2019-12-11 07:44:39
问题 I'd like to create a Login-Form using Flask and WTF. The application should read the nicknames of all users out of a database-table and display them in a QuerySelectField. It does work - but I think that I can do it in a 'nicer way'... I have a database table like this: id | nickname | fullname | email ----+-----------+----------------+-------------------------- 1 | Admin | The Admin | admin@example.com 2 | JDoe | John Doe | jd@example.com In my models.py I have the corresponding class User

Retrieve characters like Ä, ê from MySQL using Flask-Alchemy

梦想的初衷 提交于 2019-12-11 07:37:17
问题 I can't retrieve characters like ä and ê from MySQL by using Flask-SQLAlchemy. I am trying to build a website using Flask, Flask_SQLAlchemy and MySQL (5.5.3). I have taken one course in programming (Python) and the rest is self thought so I would be very happy if you could be as detailed as possible in your answers, very thankful for any advice! This link was slightly helpful, but I have problems with retrieving not storing: Flask_SQLAlchemy, MySQL, store Swedish characters å, ä, ö?