
假设我的数据模型:
class Book(Model): id = PrimaryKeyField() title = CharField(max_length=64, unique=True) author = CharField(max_length=64) publisher = CharField(max_length=64) price = DecimalField(max_digits=10, decimal_places=2) desc = CharField(max_length=256) 如果我要根据条件更新的话, 找了很多文档基本都是:
Book.update({Book.price: 29.9}).where(Book.author == '鲁迅')
但外部调用的话只能提供字段名比如 {'price': 29.9}, {'author': '鲁迅'}, 怎样才能拼出上边的语句?
1 111111111111 2020 年 7 月 2 日 via Android getattr? |
2 Trim21 2020 年 7 月 2 日 via Android {getattr(Book, key): value for key, value in updater.items()} |
5 simple2025 2020 年 7 月 2 日 peewee 支持 dict 的类型的 比如 ``` Book.update(price=29.9).where(author__eq="鲁迅") ``` |
7 vegetableChick 2020 年 7 月 3 日 condition_dict = {"author":"鲁迅", "id":1} q = Book.update({"price":30}).where( *[ getattr(Book, key) == value for key, value in condition_dict.items() ] ) q.execute() 像是这样么 ~ |