from flask import Flask
from flask_dynamo import Dynamo
import os
os.environ['AWS_ACCESS_KEY_ID'] = ''
os.environ['AWS_SECRET_ACCESS_KEY'] = ''
os.environ['AWS_REGION'] = 'ap-south-1'
app = Flask(__name__)
app.config['DYNAMO_TABLES'] = [
{
'TableName': 'users',
'KeySchema': [dict(AttributeName='username', KeyType='HASH')],
'AttributeDefinitions': [dict(AttributeName='username', AttributeType='S')],
'ProvisionedThroughput': dict(ReadCapacityUnits=5, WriteCapacityUnits=5)
}, {
'TableName': 'groups',
'KeySchema': [dict(AttributeName='name', KeyType='HASH')],
'AttributeDefinitions': [dict(AttributeName='name', AttributeType='S')],
'ProvisionedThroughput': dict(ReadCapacityUnits=5, WriteCapacityUnits=5)
}
]
app.config['DYNAMO_ENABLE_LOCAL'] = True
app.config['DYNAMO_LOCAL_HOST'] = 'localhost'
app.config['DYNAMO_LOCAL_PORT'] = 9000
dynamo = Dynamo()
当我尝试获取创建所有表时,已定义flask-dynamo的表配置并创建了dynamo实例,获取内置表错误。AttributeError AttributeError:'Dynamo'对象没有属性'tables'
@app.route('/', methods=['GET'])
def hello_world():
with app.app_context():
dynamo.create_all()
return 'table created!'
环境
python3
烧瓶发电机
提前致谢
最佳答案
这行:
dynamo = Dynamo()
应该:
dynamo = Dynamo(app)
这样,dynamo实例可以访问您在app对象上定义的表和其他配置。