SQLAlchemy instruments classes using an event-based system that intercepts all setattribute and collection mutation operations, and logs these events as things to be reconciled when the Session is flushed. Additionally, the __dict__ of the object itself is referenced by the InstanceState object which is SQLAlchemy's per- object manager. Thirdly, the InstanceState object itself is placed inside of the __dict__ of every object instance, and this cannot be shared among multiple objects. So for these three reasons and probably more, using the old Python trick of replacing __dict__ will lead to poor results.
To populate the state of an object with that of another, use a setattr() based approach: for k in dir(object1): setattr(object2, k, getattr(object1, k)) On Dec 3, 2008, at 3:47 PM, [EMAIL PROTECTED] wrote: > The reason I'm doing so, is to solve the following problem: I have an > object that is compounded from the fields - obj_id, num1, num2, num3. > obj_id is my primary key. I want to create a save method for the > object's class, that will do the following: > If the object is in the session, save it. > Else, if there is another object in the db with the same num1 and > num2, use the object in the db instead of the current one, and warn > the user if num3 is different. > So it's quite like merge, but not necessarily on the primary key. Now, > I want that "use the object in the db" wouldn't be by "returning" the > object after the merge (original object if didn't exist, and db_object > if existed), but really "replacing" it (self.dict = > existing_object.dict), so one can use that object afterwards, without > being confused. the most straightforward way to accomplish this would be a custom __new__ method, so that when you say: x = MyObject(num1=x, num2=y) the __new__() would do the session work to find the old object and return that instead, if present. There is a usage recipe on the Wiki that does something like this, except its 0.4 specific, and uses a metaclass in combination with a homegrown cache - I wrote it years ago and it can be done much more simply. Just use __new__(). --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "sqlalchemy" group. To post to this group, send email to sqlalchemy@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en -~----------~----~----~----~------~----~------~--~---