flask-sqlalchemy

How Can I Automatically Populate SQLAlchemy Database Fields? (Flask-SQLAlchemy)

旧巷老猫 提交于 2019-12-02 20:15:41
I've got a simple User model, defined like so: # models.py from datetime import datetime from myapp import db class User(db.Model): id = db.Column(db.Integer(), primary_key=True) email = db.Column(db.String(100), unique=True) password = db.Column(db.String(100)) date_updated = db.Column(db.DateTime()) def __init__(self, email, password, date_updated=None): self.email = email self.password = password self.date_updated = datetime.utcnow() When I create a new User object, my date_updated field gets set to the current time. What I'd like to do is make it so that whenever I save changes to my User

Sqlalchemy convert epoch time to date in group by

谁说胖子不能爱 提交于 2019-12-02 20:04:27
问题 I am using Sqlalchemy as ORM for PSQL db. My timestamps are stores as epoch times in my database eg, 1525868337991. (in milli sec) I am writing a query to get count of employees on a particular date(grouping by on date). I am not able to find any way by which, I can convert epoch to date in my ORM query, like psql has to_timestamp . The query is written below : employees_details = db.session.query( func.count(EmployeeInfo.id).label("employee_count"), EmployeeInfo.employee_created_on,

Defining SQLAlchemy enum column with Python enum raises “ValueError: not a valid enum”

喜夏-厌秋 提交于 2019-12-02 18:54:27
I am trying to follow this example to have an enum column in a table that uses Python's Enum type. I define the enum then pass it to the column as shown in the example, but I get ValueError: <enum 'FruitType'> is not a valid Enum . How do I correctly define a SQLAlchemy enum column with a Python enum? from flask import Flask from flask_sqlalchemy import SQLAlchemy import enum app = Flask(__name__) db = SQLAlchemy(app) class FruitType(enum.Enum): APPLE = "Crunchy apple" BANANA = "Sweet banana" class MyTable(db.Model): id = db.Column(db.Integer, primary_key = True) fruit_type = db.Column(enum

Python Flask-WTF - use same form template for add and edit operations

吃可爱长大的小学妹 提交于 2019-12-02 18:31:24
I'm just getting started with Flask / Flask-WTF / SQLAlchemy, and most example CRUD code I see shows separate templates for adding / editing. It seems repetitive to have two templates with almost identical form html (e.g. books_add.html, books_edit.html). Conceptually it makes more sense to me to have one template, something like "books_form.html", and just call render_template on that same template from two separate route definitions. I'm not quite sure how to accomplish it though, something like: @app.route('/books/add') def add_book(): ... render_template('books_form.html', action = 'add')

Accessing a mixin member variable (column) from the child

别说谁变了你拦得住时间么 提交于 2019-12-02 18:13:30
问题 I'm having trouble accessing a parent class member variable id from the child class. class BaseModel: id = db.Column(db.Integer, primary_key=True) class User(db.Model, BaseModel): username = db.Column(db.String(35), nullable=False, unique=True) followed = db.relationship( 'User', secondary=followers, primaryjoin=(followers.c.follower_id == BaseModel.id), secondaryjoin=(followers.c.followed_id == BaseModel.id), backref=db.backref('followers', lazy='dynamic'), lazy='dynamic') Gives me this

Flask SQLAlchemy querying a column with “not equals”

断了今生、忘了曾经 提交于 2019-12-02 17:53:36
I can query my Seat table for all seats where there is no invite assigned: seats = Seat.query.filter_by(invite=None).all() However, when querying for all seats that have an invite assigned, I get a NameError : seats = Seat.query.filter_by(invite!=None).all() NameError: name 'invite' is not defined Here is my Seat class: class Seat(db.Model): id = db.Column(db.Integer, primary_key=True) invite_id = db.Column(db.Integer, db.ForeignKey('invite.id')) invite = db.relationship('Invite', backref=db.backref('folks', lazy='dynamic')) How can I query for all seats where the owner is not blank? Nathan

Flask-SQLAlchemy: How to conditionally insert or update a row

强颜欢笑 提交于 2019-12-02 17:37:30
My application uses a combination of Flask, Flask-SQLAlchemy, Flask-WTF and Jinja2. In its current incarnation, I have a settings table. The table will only have one record with one field. Initially the table contains zero records. What I want to achieve is: Given that no entries exist in db, then show empty form ready for user input Given that an entry exist, show the entry, and if the user changes the value, then update the rec in db. Here is my code: models.py class Provider(db.Model): id = db.Column(db.Integer, primary_key = True) rssfeed = db.Column(db.String(120), unique = True) def _

ImportError: No module named sqlalchemy

若如初见. 提交于 2019-12-02 16:58:54
I'm unable to find a module in python ,though easy_install says its already installed. Any idea how to resolve this isseue? $ python -c "from flaskext.sqlalchemy import SQLAlchemy" Traceback (most recent call last): File "<string>", line 1, in <module> ImportError: No module named sqlalchemy $ python -V Python 2.7 $ sudo easy_install sqlalchemy Searching for sqlalchemy Best match: SQLAlchemy 0.7.7 Adding SQLAlchemy 0.7.7 to easy-install.pth file Using /usr/lib/python2.7/site-packages Processing dependencies for sqlalchemy Finished processing dependencies for sqlalchemy $ sudo pip install

Flask-SQLAlchemy Query Join relational tables

ⅰ亾dé卋堺 提交于 2019-12-02 16:25:01
I am building an app using Flask & SQLAlchemy. I basically have 3 tables: users, friendships and bestFriends: A user can have many friends but only one best friend. So I want my model to be relational. 'One-to-many' for the relation between 'users' and 'friendships' & 'one-to-one' between 'users' and 'bestFriends'. This is my Model: from app import db from sqlalchemy.orm import relationship, backref from sqlalchemy import Table, Column, Integer, ForeignKey from sqlalchemy.ext.declarative import declarative_base class users(db.Model): __tablename__ = "Users" id = db.Column(db.Integer, primary

alembic util command error can't find identifier

谁都会走 提交于 2019-12-02 16:13:53
I'm trying to use alembic to handle local migrations on my project. It worked the first time, but then I needed to delete the folder and restart.(don't ask why, I just had to) I'm following this tutorial and I run the command python manage.py db init And it was ok. But when I try to run python manage.py db migrate I'm getting this error: alembic.util.CommandError: Can't locate revision identified by '31b8ab83c7d' Now, it seems that alembic is looking for a revision that doesn't exists anymore. There is anyway to make alembic forget that file? Or like restart the comparison from None to -> auto