代码之家  ›  专栏  ›  技术社区  ›  hipsterstomper69

使用键、值对更新peewee中的模型?

  •  0
  • hipsterstomper69  · 技术社区  · 5 年前

    有没有办法在peewee中使用键、值对以编程方式更新模型?我一直在尝试通过迭代**kwargs来完成,使用以下代码:

    for key, value in kwargs.items():
                        if value is not None:
                                self.physician.update(key=value)
    

    但这给了我以下错误:

    AttributeError: type object 'RequestDetail' has no attribute 'key'
    

    有没有办法让update()方法接受key参数作为“key”的值,而不是逐字解释它?

    0 回复  |  直到 5 年前
        1
  •  0
  •   Georges Lorré    5 年前

    你能试试吗:

    self.physician.update({key:value}).execute()
    

    把它写进口述里可能会起到作用,你也可以

    self.physician.update(kwargs).execute()
    

    既然kwargs也是一个dict,但是当然,您仍然需要先删除那些不存在的值