flask-sqlalchemy

SQLAlchemy many to many relationship & Association object

孤街浪徒 提交于 2019-12-14 04:10:27
问题 I am having trouble configuring a simple relationship. I managed to get it working reasonably well with the use of a simple association table but I wanted extra 'meta' fields so I am looking into using an association object pattern. (Yes I am a DB novice) from flask.ext.sqlalchemy import SQLAlchemy from flask import Flask import os import re import datetime app = Flask(__name__) basedir = os.path.abspath(os.path.dirname(__file__)) app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' + os.path

StatementError: SQLite Date type only accepts Python date objects as input

守給你的承諾、 提交于 2019-12-14 04:05:48
问题 I am using Flask-Security, the code seems fine, but when inserting a data from register view it gives the bellow error. Since I made SECURITY_TRACKABLE = True I added some extra fields in my models and the problem might be there :( sqlalchemy.exc.StatementError StatementError: SQLite Date type only accepts Python date objects as input. (original cause: TypeError: SQLite Date type only accepts Python date objects as input.) 'INSERT INTO user (name, surname, email, password, birth_date, active,

Many-to-Many with three tables relating with each other (SqlAlchemy)

北城以北 提交于 2019-12-14 03:44:58
问题 I've three tables User, Device and Role. I have created a many-to-many relation b/w User and Device like this; #Many-to-Many relation between User and Devices userDevices = db.Table("user_devices", db.Column("id", db.Integer, primary_key=True), db.Column("user_id", db.Integer, db.ForeignKey("user.id")), db.Column("device_id", db.Integer, db.ForeignKey("device.id")))) class User(db.Model): __tablename__ = 'user' id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(60),

How to calculate cumulative moving average in Python/SQLAlchemy/Flask

核能气质少年 提交于 2019-12-14 00:21:32
问题 I'll give some context so it makes sense. I'm capturing Customer Ratings for Products in a table (Rating) and want to be able to return a Cumulative Moving Average of the ratings based on time. A basic example follows taking a rating per day: 02 FEB - Rating: 5 - Cum Avg: 5 03 FEB - Rating: 4 - Cum Avg: (5+4)/2 = 4.5 04 FEB - Rating: 1 - Cum Avg: (5+4+1)/3 = 3.3 05 FEB - Rating: 5 - Cum Avg: (5+4+1+5)/4 = 3.75 Etc... I'm trying to think of an approach that won't scale horribly. My current

sqlalchemy count related

丶灬走出姿态 提交于 2019-12-13 22:01:28
问题 I have been using django's ORM and sqlalchemy has me beat a.t.m. I have this: recipie_voters = db.Table('recipie_voters', db.Column('user_id', db.Integer, db.ForeignKey('user.id')), db.Column('recipie_id', db.Integer, db.ForeignKey('recipie.id')) ) class User(db.Model): id = db.Column(db.Integer, primary_key=True) ... class Recipie(db.Model): id = db.Column(db.Integer, primary_key=True) ... voters = db.relationship('User', secondary=recipie_voters, backref=db.backref('votes', lazy='dynamic'))

How do I provide a blog excerpt without having to show html code using Jinja2 Template?

╄→尐↘猪︶ㄣ 提交于 2019-12-13 15:01:59
问题 Currently I'm using jinja2 with flask and have stored a blog post using ckeditor in the database. The data should ideally show an image first and then following the blog posts and some other images which are linked externally to flikr. I know that I can use the {{ post.body | safe}} inside the single post view of to display the html as a real image instead of html text. However, how do I NOT show the html but show only the text excerpt in the post in the page where there are multiple links to

sqlalchemy/postgresql: database column computed default value

半城伤御伤魂 提交于 2019-12-13 14:34:23
问题 I have a transaction table, which has tenant_id and transaction_id columns (they make up a unique composite index). For insert operation, transaction_id must be incremented by 1, but for given tenant_id. So, using sqlalchemy framework, I manually find max transaction_id for tenant_id: res = db.session.query(func.max(my_tran.transaction_id).label('last_id')) \ .filter_by(tenant_id=tenant_id).one() if res.last_id: my_tran.transaction_id = res.last_id else: my_tran.transaction_id = 1 What I'd

Process Ajax request in flask

风格不统一 提交于 2019-12-13 10:17:12
问题 I've been learning flask. i've also created two projects in it. It's my third project. I'm always stuck on this point. I want to make server side code run when a event happens on page say, a button is clicked. In the linked image. I want the entry be deleted when 'delete' button is clicked. I've holding the data on mysql server, so I want it to be deleted from there as well Here is what I have done. I created a route '/delete_student' and it handles deleting the student. But the problem is it

How to run python function by clicking html button?

笑着哭i 提交于 2019-12-13 07:59:29
问题 I am trying to make this web app to work but I am getting an error. these are the steps that web app is supposed to handle: import a file run the python script export the results when I run python script independently( without interfering with flask), it works fine( I use Jupyter notebook) on the other hand, when I run it with flask (from prompt) I get an error: File "app.py", line 88, in <module> for name, df in transformed_dict.items(): NameError: name 'transformed_dict' is not defined Any

Form Data not refreshing with newest entries

别来无恙 提交于 2019-12-13 07:52:22
问题 When I add a new customer in customer table and access AddOrderForm form, I don't get the new customer in the choices.But on server restart I am able to get the new customer in the choices list.Any reason ? Customer Table class Customer(db.Model): id = db.Column(db.Integer, primary_key=True) email = db.Column(db.String(120), unique=True) mobile_num = db.Column(db.String(13), unique=True) name = db.Column(db.String(120)) marketing_source = db.Column(db.String(120)) date_of_birth = db.Column(db