Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't use post_init signal for initialize tracker #556

Merged
merged 2 commits into from
Mar 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link

@yuekui yuekui Jun 16, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could safely remove this property model_class since not using signal anymore, refer to 98f078d

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
Loading