SHARE
TWEET

Untitled

a guest Jul 21st, 2019 65 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. def clone_model(model):
  2.     """Clone an arbitrary sqlalchemy model object without its primary key values."""
  3.     # Ensure the model’s data is loaded before copying.
  4.     model.id
  5.  
  6.     table = model.__table__
  7.     non_pk_columns = [k for k in table.columns.keys() if k not in table.primary_key]
  8.     data = {c: getattr(model, c) for c in non_pk_columns}
  9.     data.pop('id')
  10.     return data
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top