def clone_model( target: Model, ignore: Optional[List[str]] = None, **kwargs: Any ) -> Model: """ Clone a SQLAlchemy model. """ ignore = ignore or [] table = target.__table__ data = { attr: getattr(target, attr) for attr in table.columns.keys() if attr not in table.primary_key.columns.keys() and attr not in ignore } data.update(kwargs) return target.__class__(**data)
def clone_model( target: Model, ignore: Optional[List[str]] = None, keep_relations: Optional[List[str]] = None, **kwargs: Any, ) -> Model: """ Clone a SQLAlchemy model. By default will only clone naive column attributes. To include relationship attributes, use `keep_relations`. """ ignore = ignore or [] table = target.__table__ primary_keys = table.primary_key.columns.keys() data = { attr: getattr(target, attr) for attr in list(table.columns.keys()) + (keep_relations or []) if attr not in primary_keys and attr not in ignore } data.update(kwargs) return target.__class__(**data)