无法识别Python的函数update()

2024-09-28 21:42:21 发布

您现在位置:Python中文网/ 问答频道 /正文

def updateTheme(self, id, new_theme):
    theme = Theme.query.filter_by(id = id).first()
    theme.update(new_theme)
    db.session.commit()

错误:“Theme”对象没有“update”属性
这就是它的工作原理:

theme = Theme(name = "osman")
new_theme = Theme(name = "miras")
theme.update(new_theme)
print(theme) # osman have to be changed to miras

即使我可以这样做:

theme.name = "miras"

当我使用多个参数,如姓名,姓氏,电话。另外updatetime并不总是提供所有参数(姓名、姓氏、电话),它应该只更新提供的参数。例如:

person = {name: "john", surname: "smith", phone: 12345, country: "USA"}
update_person = {country: "Portugal"}
person.update(update_person) # should change only country

Tags: tonameidnew参数updatethemecountry
1条回答
网友
1楼 · 发布于 2024-09-28 21:42:21

如果要更新一个或两个字段,可以使用:

theme = Theme(name = "osman")
new_theme = Theme(name = "miras")
theme.name=new_theme.name
db.session.commit()
print(theme) # osman will change to miras

如果要更新更多字段:

dict_to_update = {'name': new_theme.name}
updated_theme = Theme.query.filter_by(name="miras").update(dict_to_update)
db.session.commit()
print(theme) # osman will change to miras

相关问题 更多 >