简体   繁体   English

当我使用flask-sqlalchemy时,如何为wtforms-alchemy设置会话?

[英]how set session for wtforms-alchemy when I using flask-sqlalchemy?

I`m using flask-sqlalchemy and init SQLAlchemy() in my application by init_app() like this : 我正在通过init_app()在我的应用程序中使用flask-sqlalchemy和init SQLAlchemy(),如下所示:

in config file :

SQLALCHEMY_DATABASE_URI = 'sqlite:///example.db'
---------------------    
in extension file :

from flask.ext.sqlalchemy import SQLAlchemy

db = SQLAlchemy()
---------------------
in application file: 

#in my application file that generate my app
from ----- import db 

db.init_app(app)

my model is : 我的模型是:

from -------- import db

class User(db.Model):
    __tablename__ = 'user'
    id = db.Column(db.Integer, autoincrement=True , primary_key=True)
    username = db.Column(db.String(50),unique=True)
    password = db.Column(db.String(50))
    email = db.Column(db.String(50),unique=True)

    def __repr__(self):
        return '<User %r>' % self.username

now when i using wtforms-alchemy in my form.py like this : 现在,当我在form.py中使用wtforms-alchemy时,如下所示:

from wtforms_alchemy import ModelForm

class UserForm(ModelForm):
    class Meta:
        model = User

i have this error : 我有这个错误:

File "/usr/local/lib/python2.7/dist-packages/wtforms_components/validators.py", line 220, in _check_for_session
raise Exception('Could not obtain SQLAlchemy session.')

Exception: Could not obtain SQLAlchemy session.

How can i fix this bug ? 我该如何解决此错误?

From the WTForms-Alchemy advanced section : WTForms-Alchemy高级部分中

In order to make WTForms-Alchemy work with Flask-WTF you need the following snippet: 为了使WTForms-Alchemy与Flask-WTF配合使用,您需要以下代码段:

 from flask.ext.wtf import Form from wtforms_alchemy import model_form_factory ModelForm = model_form_factory(Form) 

The you can use the ModelForm just like before: 您可以像之前一样使用ModelForm:

 class UserForm(ModelForm): class Meta: model = User 

This does rely on User having a .query object; 确实依赖于User具有.query对象; eg your User model must be derived from db.Model . 例如,您的User模型必须从db.Model派生。 If it doesn't then you need to define get_session method on the form : 如果不是,那么您需要在表单上定义get_session方法:

class UserForm(ModelForm):
    class Meta:
        model = User

    def get_session(self):
        return db.session

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 WTForms-Alchemy 添加文本字段占位符 - How to add a text-field-placeholder using WTForms-Alchemy 如何使用 Flask-SQLAlchemy 在 Flask 中使用 Python 将当前会话设置为“登录”? - How can I set the current session as 'logged in' using Python in Flask with Flask-SQLAlchemy? 使用CSRF保护与WTForms-Alchemy - Using CSRF protection with WTForms-Alchemy 没有属性 validate_on_submit:如何在烧瓶处理程序/视图中使用 wtforms-alchemy 的 ModelForm - has no attribute validate_on_submit: How to use wtforms-alchemy's ModelForm in a flask handler / view Wtforms-alchemy字段顺序 - Wtforms-alchemy field order 如何为WTForms-Alchemy指定unique = True? - How do you specify unique=True for WTForms-Alchemy? 如何在Flask-SQLAlchemy模型上映射db.Enum模型的WTForms字段? - How do I map WTForms field for a db.Enum model on Flask-SQLAlchemy model? 如何使用 flask-sqlalchemy 获得特定值? - How do i get specific values using flask-sqlalchemy? 烧瓶 wtforms-alchemy QuerySelectField ValueError:解压的值太多(预期为 2) - flask wtforms-alchemy QuerySelectField ValueError: too many values to unpack (expected 2) 使用Flask,Flask-SQLAlchemy和SQLAlchemy进行单元测试时,如何在线程之间隔离测试数据库? - When unittesting with Flask, Flask-SQLAlchemy, and SQLAlchemy, how do I isolate test databases between threads?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM