flask SQL-Alchmey 在查询时不返回字典
flask SQL-Alchmey not returning a dictionary while querying
我对我的代码有疑问。找了几个小时,还是没搞清楚。
这是一件非常简单的事情 我正在尝试查询具有给定 ID 的 table。然后我想用传递的名称更新 'name' 属性。
但是它给我一个错误- TypeError: 'Bucket' object does not support item assignment
。 it's like its not returning a dictionary
# PUT REQUEST
def put(self, id):
bucket_to_update = bucket_db.query.get(id) #### returns <Bucket id> , not dict
print(bucket_to_update) # prints the bucket as string
if not bucket_to_update:
return {"status": "failure"}, 404
args = BucketAPI.parser.parse_args()
name = args.get('name', None)
bucket_to_update['name'] = name # >>>>>>>>>>>>>>>>>>>> PRODUCES AN ERROR
db.session.commit()
return {"status" "success"}, 200
型号 - 铲斗/bucket_db
"""Bucket model"""
from todo_app import db
class Bucket(db.Model):
__tablename__ = 'buckets'
def __init__(self, name):
self.name = name
id = db.Column(
db.Integer,
primary_key=True
)
name = db.Column(
db.Text,
index=True,
unique=True,
nullable=False
)
items = db.relationship('Item',
backref=db.backref('bucket', lazy='joined'),
lazy=True)
def __repr__(self):
return '<bucket-{}> '.format(self.name)
从 docs and link 可以清楚地看到我们可以更新 details as dictionary
,但这里不起作用。
错误日志
bucket_to_update['name'] = name
TypeError: 'Bucket' object does not support item assignment
p.s- 我正在创建资源 class 以使用 flask-restful
具有不同的 creation/deletion 方法
更新(已解决)
正如评论和答案所指出的,它是一个对象而不是字典,您应该以 bucket.name=name
的形式访问它。另外,我在返回时漏掉了一个冒号。
在def put(self, id):
末尾添加(3),
将 return {"status" "success"}, 200
更正为 return {"status":"success"}, 200
(1) 在你的 class Bucket(db.Model):
中你定义了初始化函数 __init__
所以你覆盖了父 class db.Model
的初始化函数,所以 __init__
父函数 class 将不会被调用。
因此,尝试:
def __init__(self, name):
db.Model.__init__(self) ## *** invoke __init__ of the parent class
self.name = name
(2)更新在bucket_to_update
中的修改:
bucket_to_update['name'] = name # >>>>>>>>>>>>>>>>>>>> PRODUCES AN ERROR
db.session.commit()
提交前添加db.session.add(bucket_to_update)
:
bucket_to_update['name'] = name # >>>>>>>>>>>>>>>>>>>> PRODUCES AN ERROR
db.session.add(bucket_to_update)
db.session.commit()
期待您的评论。
祝你好运。
我无法访问我的编码环境,但我认为如果您按照它进行操作,它应该可以正常工作。
bucket_to_update.name = name
我对我的代码有疑问。找了几个小时,还是没搞清楚。
这是一件非常简单的事情 我正在尝试查询具有给定 ID 的 table。然后我想用传递的名称更新 'name' 属性。
但是它给我一个错误- TypeError: 'Bucket' object does not support item assignment
。 it's like its not returning a dictionary
# PUT REQUEST
def put(self, id):
bucket_to_update = bucket_db.query.get(id) #### returns <Bucket id> , not dict
print(bucket_to_update) # prints the bucket as string
if not bucket_to_update:
return {"status": "failure"}, 404
args = BucketAPI.parser.parse_args()
name = args.get('name', None)
bucket_to_update['name'] = name # >>>>>>>>>>>>>>>>>>>> PRODUCES AN ERROR
db.session.commit()
return {"status" "success"}, 200
型号 - 铲斗/bucket_db
"""Bucket model"""
from todo_app import db
class Bucket(db.Model):
__tablename__ = 'buckets'
def __init__(self, name):
self.name = name
id = db.Column(
db.Integer,
primary_key=True
)
name = db.Column(
db.Text,
index=True,
unique=True,
nullable=False
)
items = db.relationship('Item',
backref=db.backref('bucket', lazy='joined'),
lazy=True)
def __repr__(self):
return '<bucket-{}> '.format(self.name)
从 docs and link 可以清楚地看到我们可以更新 details as dictionary
,但这里不起作用。
错误日志
bucket_to_update['name'] = name
TypeError: 'Bucket' object does not support item assignment
p.s- 我正在创建资源 class 以使用 flask-restful
具有不同的 creation/deletion 方法更新(已解决)
正如评论和答案所指出的,它是一个对象而不是字典,您应该以 bucket.name=name
的形式访问它。另外,我在返回时漏掉了一个冒号。
在def put(self, id):
末尾添加(3),
将 return {"status" "success"}, 200
更正为 return {"status":"success"}, 200
(1) 在你的 class Bucket(db.Model):
中你定义了初始化函数 __init__
所以你覆盖了父 class db.Model
的初始化函数,所以 __init__
父函数 class 将不会被调用。
因此,尝试:
def __init__(self, name):
db.Model.__init__(self) ## *** invoke __init__ of the parent class
self.name = name
(2)更新在bucket_to_update
中的修改:
bucket_to_update['name'] = name # >>>>>>>>>>>>>>>>>>>> PRODUCES AN ERROR
db.session.commit()
提交前添加db.session.add(bucket_to_update)
:
bucket_to_update['name'] = name # >>>>>>>>>>>>>>>>>>>> PRODUCES AN ERROR
db.session.add(bucket_to_update)
db.session.commit()
期待您的评论。
祝你好运。
我无法访问我的编码环境,但我认为如果您按照它进行操作,它应该可以正常工作。
bucket_to_update.name = name