Skip to content

Instantly share code, notes, and snippets.

@kezabelle
Last active August 3, 2016 13:56
Show Gist options
  • Save kezabelle/b6a7070bbd4a2edc6ef43334eb2a04e8 to your computer and use it in GitHub Desktop.
Save kezabelle/b6a7070bbd4a2edc6ef43334eb2a04e8 to your computer and use it in GitHub Desktop.
for tga in #django IRC.
@contextmanager
def copyable(obj):
oldval = copy(obj.pk)
obj.pk = None
yield obj
obj.pk = oldval
with copyable(obj) as tosave: # I might've forgotten the as part initially.
newobj = tosave.save()
print(obj.pk)
print(newobj.pk)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment