not a shorter way, but more correct in case you have columns mapped to
different names:
mp1 = inspect(instance1)
mp2 = inspect(Class2)
Class2(**dict(
zip(
mp2.get_property_by_column(col).key for col in mp2.primary_key,
mp1.primary_key_from_instance(instance1)
)
))
On Aug 27, 2014, at 9:24 PM, Gerald Thibault <[email protected]> wrote:
> # instance1 is an instance of Class1, i want to create Class2 with the same pk
>
> keys = [key.name for key in class_mapper(Class2).primary_key]
> cls, values = identity_key(instance=instance1)
> kwargs = dict(zip(keys, values))
> instance2 = Class2(**kwargs)
--
You received this message because you are subscribed to the Google Groups
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.