我在别处读到 Dokku 不支持 SQLite。为什么不?我正在使用带有 SQLAlchemy 的 Flask 应用程序,这似乎是所有抽象发生的地方。我不能将 sqlite 数据库文件放在磁盘上的某个位置(/home/dokku/database/my_db,也许?)并将其提供给 SQLAlchemy?
engine = create_engine('sqlite://///home/dokku/database/my_db')
Run Code Online (Sandbox Code Playgroud)
更具体地说,我会使用 Dokku 将该字符串存储为环境变量,而不是直接将其传入。
为什么这行不通?
将我的模型移动到它们自己的模块会导致 InvalidRequestError .. 这似乎与数据库会话和我在模型模块中导入 db 的方式有关。
我试图将这个问题减少到必要的最少代码量。首先,一个可用的 Flask 应用程序:
import os
from flask import Flask, render_template, request
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
app.config['SECRET_KEY'] = 'somesecret'
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' + os.path.join(app.instance_path, 'app.db')
db = SQLAlchemy(app)
class Thing(db.Model):
id = db.Column(db.Integer, primary_key=True)
text = db.Column(db.String(300), nullable=False)
user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
def __init__(self, user, text):
self.user = user
self.text = text
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(80), unique=True)
things = db.relationship('Thing', backref='user', lazy='dynamic')
def __init__(self, username):
self.username …Run Code Online (Sandbox Code Playgroud) 来自 Django,我很难弄清楚多对多关系是如何工作的。
以下models.py 不起作用,我收到错误消息:InvalidRequestError:一个或多个映射器无法初始化 - 无法继续初始化其他映射器。原始异常是:在关系“Sector.companies”上创建反向引用“扇区”时出错:映射器“Mapper|Company|companys”上存在该名称的属性
代码如下:
`
company_contacts = db.Table('company_contacts',
db.Column('company_id', db.Integer, db.ForeignKey('companies.id')),
db.Column('contact_id', db.Integer, db.ForeignKey('contacts.id'))
)
company_sectors = db.Table('company_sectors',
db.Column('company_id', db.Integer, db.ForeignKey('companies.id')),
db.Column('sector_id', db.Integer, db.ForeignKey('sectors.id'))
)
company_worklists = db.Table('company_worklists',
db.Column('company_id', db.Integer, db.ForeignKey('companies.id')),
db.Column('worklist_id', db.Integer, db.ForeignKey('worklists.id'))
)
class Sector(db.Model):
__tablename__ = 'sectors'
id = db.Column(db.Integer, primary_key = True)
name_srb= db.Column(db.String(64), unique = True)
name_ita= db.Column(db.String(64), unique = True)
companies = db.relationship('Company',
secondary = company_sectors,
backref = db.backref('sectors', lazy = 'dynamic'),
lazy = 'dynamic')
class Contact(db.Model):
__tablename__ = 'contacts' …Run Code Online (Sandbox Code Playgroud) 我目前正在使用 Flask 和 SQLAlchemy 进行一个项目,基本上是像 discourse 这样的论坛软件,但使用的是 Python 3。
目前有两种模式,论坛和线程
我有一个 Jinja 模板,应该可以生成论坛主题的表格记录,请参见下文。
{% for thread in Forum.ForumThreads.filter_by(TagID.in_(TagsToShow)): %}
<TR Class="ThreadRecord">
<TD><a href="{{thread.ForumID}}/Thread/{{thread.ThreadID}}">{{thread.Title}}</a></TD>
<TD class="ThreadTag{{thread.TagID}}">{{thread.Tag.Name}}</TD>
<TD>{{thread.PostList.count()}}</TD>
<TD>{{thread.User.Name}}</TD>
<TD>{{thread.CreationDate}}</TD>
</TR>
{% endfor %}
Run Code Online (Sandbox Code Playgroud)
这由以下视图调用(稍微简化)。
@app.route('/Forum/<int:URLForumID>/ForumThreads')
def ThreadsTable(URLForumID):
TagsToShow = (1,2,3,4)
Forum = models.Forum.query.filter_by(ForumID=URLForumID).first()
return flask.render_template('ForumThreads.html', Forum=Forum, TagsToShow=TagsToShow)
Run Code Online (Sandbox Code Playgroud)
但是,每次我尝试运行它时,都会收到错误“jinja2.exceptions.UndefinedError: 'TagID' is undefined”。
我试过用{% for thread in Forum.ForumThreads.filter_by(TagID=1): %}运行它,它似乎运行良好,所以我的问题似乎在于我如何调用 .in_() 方法。我已经搜索了 SQLalchemy的文档,但一直找不到答案,有人能指出我正确的方向吗?
我不知道它是否有帮助,但下面是使用的 SQLalchemy 模型的两个精简版本。
class Forum(db.Model):
__tablename__ = "Forum"
ForumID = db.Column(db.Integer, primary_key=True)
ForumName = db.Column(db.Unicode(20), …Run Code Online (Sandbox Code Playgroud) 我需要使用flask-sqlalchemy将两个模型员工和技能与额外数据相关联。
例如,用户1可以做skill1用效率为100%和skill2用效率80%。
所以,我制作了 3 个模型:
class Employee(db.Model):
__tablename__ = 'employees'
id = db.Column(db.Integer, primary_key=True)
login = db.Column(db.String(64), index=True, unique=True, nullable=False)
skills = db.relationship('Skill', secondary='employee_skills',
backref=db.backref('employees', lazy='dynamic'))
def __repr__(self):
return '<Employee %r, Skills: %r>' % (self.login, self.skills)
class Skill(db.Model):
__tablename__ = 'skills'
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(64), nullable=False, unique=True)
def __repr__(self):
return '<Skill %r>' % self.name
class EmployeeSkill(db.Model):
__tablename__ = 'employee_skills'
employee_id = db.Column(db.Integer, db.ForeignKey('employees.id'), primary_key=True)
skill_id = db.Column(db.Integer, …Run Code Online (Sandbox Code Playgroud) 我正在寻找Column定义的文档,我想知道该函数Column有多少个参数。
https://pythonhosted.org/Flask-SQLAlchemy/models.html#simple-example
http://flask-sqlalchemy.pocoo.org/2.0/api/#sessions
示例代码:
class User(db.Model):
username = db.Column(db.String(80), unique=True)
pw_hash = db.Column(db.String(80))
Run Code Online (Sandbox Code Playgroud)
而我什么也没找到。
如何在 Flask 应用程序中处理和引用多个模型?
是否有任何原因我不能拥有多个模型类、.py文件?而不是一个 big models.py,有没有办法在烧瓶中有以下内容:
示例模型:
students.py
teachers.py
classes.py
schedules.py
...
Run Code Online (Sandbox Code Playgroud)
?
I'm trying to do unit testing of my Flask web app. I'm use a pattern I saw in a Udemy class on Flask and a pattern similar to the Flask Mega-Tutorial online (http://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-vii-unit-testing). The problem I'm having is that the test does not actual create it's own database -- rather it uses the production database and messes it up.
Here's what tests.py script looks like:
import os,sys
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
basedir = os.path.abspath(os.path.dirname(__file__))
import unittest
from myapp import app, …Run Code Online (Sandbox Code Playgroud) 我正在使用 Flask-SQLAlchemy 和以下内容models.py:
class DomainRoot(db.Model):
# Int/Float fields
id = db.Column(db.Integer, primary_key=True)
# String fields
domain = db.Column(db.String(64), index=True, unique=True)
# DateTime fields
created = db.Column(db.DateTime)
updated = db.Column(db.DateTime)
# ForeignKey relationships
domain_paths = db.relationship('DomainPath', backref='domain_root', lazy='dynamic')
def __repr__(self):
return '<DomainRoot %r>' % (self.domain)
class DomainPath(db.Model):
# Int/Float fields
id = db.Column(db.Integer, primary_key=True)
# String fields
domain_path = db.Column(db.String(256), index=True, unique=True)
# DateTime fields
created = db.Column(db.DateTime)
updated = db.Column(db.DateTime)
# ForeignKey fields
domainroot_id = db.Column(db.Integer, db.ForeignKey('domainroot.id'))
def …Run Code Online (Sandbox Code Playgroud) flask-sqlalchemy ×10
flask ×7
python ×7
sqlalchemy ×7
sqlite ×2
dokku ×1
model ×1
python-3.x ×1