On Oct 16, 2010, at 2:03 PM, Michael Hipp wrote:

> On 10/16/2010 12:52 PM, Michael Bayer wrote:
>> 
>> On Oct 16, 2010, at 1:02 PM, Michael Hipp wrote:
>> 
>>> On 8/24/2010 9:47 PM, Michael Bayer wrote:
>>>> Michael Hipp wrote:
>>>>> How do I make a copy of an orm object such that modifications to the
>>>>> copy do not affect the original?
>>>> 
>>>> The mapped object has a member "_sa_instance_state" that you basically
>>>> don't want to transfer to your new object.    You want it to have its own
>>>> "_sa_instance_state" and this comes from calling the plain constructor,
>>>> which the "copy" module, if that's what you're using, does not use.   You
>>>> also want to set attributes normally, not populating __dict__ directly.
>>>> So just basically don't use the "copy" module.
>>>> 
>>>>     x = MyObject()
>>>>     for a in dir(myoldobject):
>>>>         if not a.startswith('_'):
>>>>             setattr(x, a, getattr(myoldobject, a))
>>> 
>>> Resurrecting an old thread ...
>>> 
>>> I'm just now getting around to try this but I'm finding out it doesn't 
>>> really work like I'd hoped.
>>> 
>>> As soon as 'getattr' hits a column with a ForeignKey it immediately tries 
>>> to autoflush INSERT 'x'. But 'x' is only half-baked and not ready to be 
>>> saved.
>>> 
>>> In fact, I don't ever want to save 'x', and I especially don't want to 
>>> INSERT it. It would be a duplicate of 'myoldobject'.
>>> 
>>> Is there a way to copy an orm object and tell it "don't ever save this I 
>>> just want to keep it around to look at"?
>>> 
>>> Alternatively I can just copy all the attributes to a dict(), but that's a 
>>> bit messy.
>> 
>> dont put it in the Session.
> 
> That makes sense. But how do I not do that? As in your example code above I'm 
> not adding it to a session, at least not intentionally.

don't call Session.add().  the code above doesn't add to a session unless 
you're using some special extensions like Session.mapper.



> 
> Thanks,
> Michael
> 
> -- 
> You received this message because you are subscribed to the Google Groups 
> "sqlalchemy" group.
> To post to this group, send email to sqlalch...@googlegroups.com.
> To unsubscribe from this group, send email to 
> sqlalchemy+unsubscr...@googlegroups.com.
> For more options, visit this group at 
> http://groups.google.com/group/sqlalchemy?hl=en.
> 

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to sqlalch...@googlegroups.com.
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.

Reply via email to