Comments (5)
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.
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.
@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.
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.
I created a PR for this issue.
from django-lifecycle.
Related Issues (20)
- has_changed incompatible with model with vector_fields HOT 5
- Change in #121 breaks subsequent model updates in on_commit=True hooks (infinite recursion) HOT 8
- skip_hooks context manager HOT 1
- Hooks not effecting rows when called using create_or_update in django 4.2 HOT 2
- Feature request: Trigger method when *all* fields have been updated. HOT 2
- Deploy documentation (gh-pages) when a release is created
- For hooks, "was" should track change since last save, not initial value HOT 5
- AFTER_DELETE hook not triggered on when admin page "Delete selected objects" is used on the list view HOT 2
- Add a common gotchas or FAQ in the documentation HOT 3
- many to many changes HOT 1
- What is django_lifecycle_checks? And can it also perhaps be added to the main codebase? HOT 7
- How to add a hook which uses subclass values for list of fields ? HOT 1
- Improve django check by checking if `LifecycleModelMixin` precedes `models.Model` in the MRO
- `_DELETE` events not triggered by admin delete batch action HOT 1
- Can't import LifecycleModel or LifecycleMixin in v1.2.0 HOT 5
- Update documentation to show examples with `condition` hook parameter
- Use of deepcopy to capture mutable field-changes breaks some models HOT 3
- 'NoneType' object has no attribute 'seek' on saving a model with ImageField HOT 5
- refresh_from_db doesn't update the initial state HOT 1
- Allow optional extra context to be passed through to lifecycle hooks
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from django-lifecycle.