Skip to content

Commit

Permalink
Merge pull request #556 from meanmail/master
Browse files Browse the repository at this point in the history
Don't use `post_init` signal for initialize tracker
  • Loading branch information
foarsitter authored Mar 21, 2024
2 parents 46d3392 + 2bbbfcb commit 473ee74
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 5 deletions.
1 change: 1 addition & 0 deletions AUTHORS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,4 @@
| Éric Araujo <[email protected]>
| Őry Máté <[email protected]>
| Nafees Anwar <[email protected]>
| meanmail <[email protected]>
7 changes: 6 additions & 1 deletion CHANGES.rst
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
Changelog
=========

4.5.0
-----

- Don't use `post_init` signal for initialize tracker

4.4.0 (2024-02-10)
------------------

- Add support for `Python 3.11`
- Add support for `Python 3.11`
- Add support for `Python 3.12`
- Drop support for `Python 3.7`
- Add support for `Django 4.2`
Expand Down
6 changes: 3 additions & 3 deletions docs/utilities.rst
Original file line number Diff line number Diff line change
Expand Up @@ -328,9 +328,9 @@ FieldTracker implementation details
This is how ``FieldTracker`` tracks field changes on ``instance.save`` call.

1. In ``class_prepared`` handler ``FieldTracker`` patches ``save_base`` and
``refresh_from_db`` methods to reset initial state for tracked fields.
2. In ``post_init`` handler ``FieldTracker`` saves initial values for tracked
1. In ``class_prepared`` handler ``FieldTracker`` patches ``save_base``,
``refresh_from_db`` and ``__init__`` methods to reset initial state for tracked fields.
2. In the patched ``__init__`` method ``FieldTracker`` saves initial values for tracked
fields.
3. ``MyModel.save`` changes ``update_fields`` in order to store auto updated
``modified`` timestamp. Complete list of saved fields is now known.
Expand Down
12 changes: 11 additions & 1 deletion model_utils/tracker.py
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ def finalize_class(self, sender, **kwargs):
wrapped_descriptor = wrapper_cls(field_name, descriptor, self.attname)
setattr(sender, field_name, wrapped_descriptor)
self.field_map = self.get_field_map(sender)
models.signals.post_init.connect(self.initialize_tracker)
self.patch_init(sender)
self.model_class = sender
setattr(sender, self.name, self)
self.patch_save(sender)
Expand All @@ -356,6 +356,16 @@ def initialize_tracker(self, sender, instance, **kwargs):
tracker.set_saved_fields()
instance._instance_initialized = True

def patch_init(self, model):
original = getattr(model, '__init__')

@wraps(original)
def inner(instance, *args, **kwargs):
original(instance, *args, **kwargs)
self.initialize_tracker(model, instance)

setattr(model, '__init__', inner)

def patch_save(self, model):
self._patch(model, 'save_base', 'update_fields')
self._patch(model, 'refresh_from_db', 'fields')
Expand Down

0 comments on commit 473ee74

Please sign in to comment.