Функция Python update () не распознается - PullRequest
0 голосов
/ 11 декабря 2018
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"

Когда мне нужно использовать несколько параметров, таких как имя, фамилия, телефон.Также updateTheme не всегда предоставляет все параметры (имя, фамилия, телефон) и должна обновлять только предоставленные параметры.Например:

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

1 Ответ

0 голосов
/ 11 декабря 2018

Если вы хотите обновить одно или два поля, вы можете использовать:

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
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...