[Python] How to clone a SQLAlchemy object with new primary key

Discussão em 'Python' iniciado por Stack, Setembro 3, 2025.

  1. Stack

    Stack Membro Participativo

    I want to clone a SQLAlchemy object:

    I tried:

    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)


    but it just updates the old object.

    Here is the code of products.save function:

    class Service(object):

    __model__ = None

    def save(self, model):
    self._isinstance(model)
    db.session.add(model)
    db.session.commit()
    return model

    Continue reading...

Compartilhe esta Página