我想更改Flask-SQLAlchemy中设置的约束的标准命名约定,以简化使用Alembic的迁移.
由于Flask-SQLAlchemy自己创建了声明性基础(你可以提供命名约定),我不确定这样做的最佳方法是什么.我可以在创建声明性基础之后更改命名约定(即,当我创建Flask-SQLAlchemy对象时)或者是否必须继承SQLAlchemy类?有完全不同的方式吗?
当我dev_appserver.py .在app.py使用Python 2.7.12的virtualenv中运行(在包含该目录的目录中)时,我收到此错误并输出:
(.venv)$ dev_appserver.py .
INFO 2017-02-21 18:54:47,250 devappserver2.py:764] Skipping SDK update check.
INFO 2017-02-21 18:54:47,273 api_server.py:268] Starting API server at: http://localhost:35473
INFO 2017-02-21 18:54:47,276 dispatcher.py:199] Starting module "default" running at: http://localhost:8080
INFO 2017-02-21 18:54:47,276 admin_server.py:116] Starting admin server at: http://localhost:8000
Traceback (most recent call last):
File "/opt/gcloud/google-cloud-sdk/platform/google_appengine/_python_runtime.py", line 101, in <module>
_run_file(__file__, globals())
File "/opt/gcloud/google-cloud-sdk/platform/google_appengine/_python_runtime.py", line 97, in _run_file
execfile(_PATHS.script_file(script_name), globals_)
File "/opt/gcloud/google-cloud-sdk/platform/google_appengine/google/appengine/tools/devappserver2/python/runtime.py", line 185, in <module>
main()
File "/opt/gcloud/google-cloud-sdk/platform/google_appengine/google/appengine/tools/devappserver2/python/runtime.py", line 165, in main …Run Code Online (Sandbox Code Playgroud) 当我使用 WTForms-JSON 时,嵌套表单 (FormFields) 不会填充数据。我无法发现我的错误,请参见下面的示例。
from flask import Flask, request, jsonify
from flask_wtf import Form
from wtforms import TextField, FormField, IntegerField
from wtforms.validators import InputRequired
import wtforms_json
app = Flask(__name__)
app.config["WTF_CSRF_ENABLED"] = False
wtforms_json.init()
class Address(Form):
street = TextField('street', validators=[InputRequired()])
number = IntegerField('number', validators=[InputRequired()])
class User(Form):
name = TextField('name', validators=[InputRequired()])
address = FormField(Address, label='address')
@app.route('/', methods=['POST'])
def why_no_work():
form = User()
form.from_json(request.json)
print form.data
if form.validate():
return jsonify(success='YEAH')
else:
return jsonify(errors=form.errors)
if __name__ == '__main__':
app.run(debug=True)
Run Code Online (Sandbox Code Playgroud)
我发送以下 JSON 请求
{ …Run Code Online (Sandbox Code Playgroud) user下面是使用夹具来设置测试的测试代码示例。
@pytest.fixture
def user():
# Setup db connection
yield User('test@example.com')
# Close db connection
def test_change_email(user):
new_email = 'new@example.com'
change_email(user, new_email)
assert user.email == new_email
Run Code Online (Sandbox Code Playgroud)
如果我想添加批量更改用户电子邮件的功能并且在测试前需要设置 10 个用户,有没有办法使用相同的固定装置在同一测试中生成多个用户对象?
python ×4
flask ×2
pytest ×1
python-2.7 ×1
sqlalchemy ×1
virtualenv ×1
wtforms ×1
wtforms-json ×1