Skip to content

Applying serializer updates to model without committing? #8570

Discussion options

You must be logged in to vote

I ended up going with the transaction rollback solution and it worked out surprisingly well.

  1. Store the original object and make a copy of it
  2. Modify the copy in a transaction, then raise an exception to rollback the transaction
  3. Restore the original object
  4. You now have both the original (restored) object and the modified one.

Code example:

from copy import copy
from django.db import transaction

class ExitTransaction(Exception):
    pass

# ...

# Make a copy of the original so we can restore it later. Rolling back a
# transaction doesn't undo the changes to the instance.
original = instance.obj
instance.obj = copy(original)

try:
    with transaction.atomic():
        # Modify instance.o…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@voxvici
Comment options

Answer selected by Kangaroux
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants