Hi there,
I'd like to override __deepcopy__
for a given SQLAlchemy-mapped class such that it ignores any SQLA attributes but deepcopies everything else that's part of the class.
I'm not particularly familiar with overriding any of Python's built-in objects in particular but I've got some idea as to what I want.
Let's just make a very simple class User
that's mapped using SQLA.
class User(object):
def __init__(self, user_id=None, name=None):
self.user_id = user_id
self.name = name
I've used dir()
to see, before and after mapping, what SQLAlchemy-specific attributes there are and I've found _sa_class_manager
and _sa_instance_state
.
Questions
Provided those are the only ones how would I ignore that when defining __deepcopy__
?
Also, are there any attributes the SQLA injects into the mapped object?
(I asked this in a previous question (as an edit a few days after I selected an answer to the main question, though) but I think I missed the train there. Apologies for that.)
Edit - Fixed code thanks to zifot's answer
The only thing I got out of the Python docs is that you need to define deepcopy with memo
as an extra argument. After a teensy bit of digging around I tried this out:
def __deepcopy__(self, memo):
dpcpy = self.__class__()
memo[id(self)] = dpcpy
for attr in dir(self):
if not attr.startswith('_'):
value = getattr(self, attr)
setattr(dpcpy, attr, copy.deepcopy(value, memo))
return dpcpy
Then I created an instance of User
as:
snake = User(913, 'Snake,S.')
After that, I tried a deepcopy
operation as:
snake_dc = copy.deepcopy(snake)
...and snake_dc
still has the SQLA attributes in it...
I'm all open to help, suggestions, etc.