Skip to content

Instantly share code, notes, and snippets.

@kennyledet
Created July 19, 2014 20:50
Show Gist options
  • Save kennyledet/a6281d5ea70c50b5e731 to your computer and use it in GitHub Desktop.
Save kennyledet/a6281d5ea70c50b5e731 to your computer and use it in GitHub Desktop.
SQLAlchemy Duplicate Row
def copy_row(model, row, ignored_columns=[]):
copy = model()
for col in row.__table__.columns:
if col.name not in ignored_columns:
try:
copy.__setattr__(col.name, getattr(row, col.name))
except Exception as e:
print e
continue
return copy
@NiIkLaAs
Copy link

Simpler without passing model in argument :

def copy_row(row, ignored_columns=[]):
    copy = type(row)()
    for col in row.__table__.columns:
        if col.name not in ignored_columns:
            try:
                copy.__setattr__(col.name, getattr(row, col.name))
            except Exception as e:
                print e
                continue
    return copy

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment