flask-sqlalchemy

Flask with create_app, SQLAlchemy and Celery

天涯浪子 提交于 2019-12-09 04:03:43
问题 I'm really struggling to the get the proper setup for Flask, SQLAlchemy and Celery. I have searched extensively and tried different approaches, nothing really seems to work. Either I missed the application context or can't run the workers or there are some other problems. The structure is very general so that I can build a bigger application. I'm using: Flask 0.10.1, SQLAlchemy 1.0, Celery 3.1.13, my current setup is the following: app/__init__.py #Empty app/config.py import os basedir = os

Flask - WTForm - save form to db

◇◆丶佛笑我妖孽 提交于 2019-12-09 01:29:07
问题 I have problem with saving form (wtf) to (sqlalchemy) db ,form is rendering ,but after submit nothing happens ... e.g. : 127.0.0.1 - - [30/Dec/2013 10:30:24] "POST /add/ HTTP/1.1" 200 - I was trying to save without validators e.g. : name = TextField('Task Name') and i was trying to save form other way : if request.method == 'POST' and form.validate(): new_task = Tasks( form.name.data, form.due_date.data, form.priority.data, form.posted_date.data, session['user_id'], form.category.data, form

How does Flask-SQLAlchemy create_all discover the models to create?

℡╲_俬逩灬. 提交于 2019-12-08 22:58:28
问题 Flask-SQLAlchemy's db.create_all() method creates each table corresponding to my defined models. I never instantiate or register instances of the models. They're just class definitions that inherit from db.Model . How does it know which models I have defined? 回答1: Flask-SQLAlchemy does nothing special, it's all a standard part of SQLAlchemy. Calling db.create_all eventually calls db.Model.metadata.create_all. Tables are associated with a MetaData instance as they are defined. The exact

sqlalchemy conditional multiple filters on dynamic lazy relationship

醉酒当歌 提交于 2019-12-08 17:45:29
问题 I am using sqlalchemy with the following models class Page(db.Model): id= .. posts = db.relationship('Post', lazy='dynamic') class Post(db.Model): id=.. page_id=.. author= db.Column(db.String) date= db.Column(db.DateTime) in the Page class I have a method to get the page's posts for a specific date and author, it looks like that def author_posts(author, start_date=None, end_date=None): p= self.posts.filter(Post.author == author) if start_date: p.filter(Post.date >= start_date) if end_date: p

SQLAlchemy extension isn't registered when running app with Gunicorn

吃可爱长大的小学妹 提交于 2019-12-08 17:20:50
问题 I have an application that works in development, but when I try to run it with Gunicorn it gives an error that the "sqlalchemy extension was not registered". From what I've read it seems that I need to call app.app_context() somewhere, but I'm not sure where. How do I fix this error? # run in development, works python server.py # try to run with gunicorn, fails gunicorn --bind localhost:8000 server:app AssertionError: The sqlalchemy extension was not registered to the current application.

How to model a `UNIQUE` constraint in SQLAlchemy?

前提是你 提交于 2019-12-08 15:29:42
问题 I am writing a Flask/SQLAlchemy application in which I have users and groups. Users can belong to several groups, and they have a unique number within each group . Asking about how to model the database I was advised to use the following table structure for my many-to-many relationship: TABLE UserGroups GroupID UserID UserNumber PRIMARY KEY (GroupID, UserID) UNIQUE (GroupID, UserNumber) FOREIGN KEY (GroupID) REFERENCES Groups (GroupID) FOREIGN KEY (UserID) REFERENCES Users (UserID) Now I know

Insert JSOB object into Postgres database

血红的双手。 提交于 2019-12-08 13:15:20
问题 I'm trying to insert an number of JSON objects in to a Postgres database. No error is raised but the fields in the database remain blank. I have tried many of things. using python dicts, using json.dumps, saving as a string and all return blank. The fields test_record_parse_data, test_parse_data, test_record_mappings and test_mappings are key value pairs from the http POST request. key being the name of the field and value being the value. Below is my code in its current state. from

Flask SQLAlchemy - 2013 Lost Connection

你说的曾经没有我的故事 提交于 2019-12-08 10:24:24
问题 So I'm attempting a build a site dynamically using a database and flask. The site works how I want it to as of right now, but only for about a minute before I get an error message saying: sqlalchemy.exc.OperationalError: (pymysql.err.OperationalError) (2013, 'Lost connection to MySQL server during query') The database contains 4 entries I want to grab for each page, and one main page where I only grab 2 entries from each entry in the database. Somewhere along the line it run into an error

Flask-SQLAlchemy wtform based on db

扶醉桌前 提交于 2019-12-08 09:54:49
问题 My app is working on my main PC. I tried to clone the app to my laptop (I tried to initialize it on PC in a different directory and the problem was the same so the problem isn't with the laptop) and when initializing the db with flask db init I got the following error: sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) no such table: race [SQL: 'SELECT race.id AS race_id, race.name AS race_name \nFROM race'] (Background on this error at: http://sqlalche.me/e/e3q8) What I'm trying to

SQLAlchemy: Sum all Child column attributes while querying Parent?

十年热恋 提交于 2019-12-08 09:35:35
问题 I have a many-to-one relationship between Team and League The following code joins the tables, and orders the leagues by goals_for for each team in that league : for league in League.query.join(League.teams).order_by(desc(Team.goals_for)): total_goals = 0 for team in league.teams: total_goals += team.goals_for print("Total goals scored in", league.full_name, "is", total_goals) Correctly produces: Total goals scored in Germany Bundesliga is 22 Total goals scored in English Premier League is 15