本文介绍了如何使用新主键克隆SQLAlChemy对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我要克隆SQLAlChemy对象:
我已尝试:
product_obj = products.all()[0] #here products is service name product_obj.product_uid = 'soemthing' #here product_uid is the pk of product model products.save(product_obj)但它只更新旧对象。
以下是products.save函数的代码:
class Service(object): __model__ = None def save(self, model): self._isinstance(model) db.session.add(model) db.sessionmit() return model 推荐答案这应该可以工作:
product_obj = products.all()[0] db.session.expunge(product_obj) # expunge the object from session make_transient(product_obj) # docs.sqlalchemy/en/rel_1_1/orm/session_api.html#sqlalchemy.orm.session.make_transient product_obj.product_uid = 'something' db.session.add(product_obj)更多推荐
如何使用新主键克隆SQLAlChemy对象
发布评论