Giter Club home page Giter Club logo

Comments (5)

alb3rto269 avatar alb3rto269 commented on August 22, 2024

I found that moving the self._initial_state = self._snapshot_state() at the end of the save method to a on commit callback works for hooks with on_commit=True and on_commit=False.

I can work on a PR for this if you think it is the right path.

from django-lifecycle.

amatmv avatar amatmv commented on August 22, 2024

Hi! Just faced the same error today. Then came in here and saw that this was the last open issue 😂

Although I don't understand what you're saying here @alb3rto269 🤔

moving the self._initial_state = self._snapshot_state() at the end of the save method works

This line is already at the end of the save method.
Maybe this could be an approach:

    def run_save(self, *args, **kwargs):
        # Run before_* hooks
        super().save(*args, **kwargs)
        # Run after_* hooks
        
    def save(self, *args, **kwargs):
        with transaction.atomic():
            self.run_save(*args, **kwargs)

        self._initial_state = self._snapshot_state()

from django-lifecycle.

alb3rto269 avatar alb3rto269 commented on August 22, 2024

@amatmv My bad. I just fixed my comment.

I meant moving the last line of the save method to a on-commit callback.

Notice that according Django's docs, on-commit callbacks are executed in the order they are registered. Therefore, we can ensure that this new "reset_state" callback will be executed after any other callback registered by the AFTER_* hooks.

Something like this:

def reset_state(self):
    self._initial_state = self._snapshot_state()

def save(self, *args, *kwargs):
    # run before_* hooks
    save(*args, **kwargs)
    # run after_* hooks
    transaction.on_commit(self.reset_state)

from django-lifecycle.

amatmv avatar amatmv commented on August 22, 2024

Yes! It seems also a good approach. @alb3rto269
I'll be happy to take a look if you make a PR for this.

from django-lifecycle.

alb3rto269 avatar alb3rto269 commented on August 22, 2024

I created a PR for this issue.

from django-lifecycle.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.