如何使用新主键克隆SQLAlChemy对象

编程入门 行业动态 更新时间:2024-10-28 21:23:15
本文介绍了如何使用新主键克隆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对象

本文发布于:2023-10-16 06:54:38,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1496800.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何使用   主键   对象   SQLAlChemy

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!