From 593fa26f5576a51359727782508fc013ebc319c8 Mon Sep 17 00:00:00 2001 From: MaximZemskov Date: Mon, 27 Jul 2020 22:48:36 +0300 Subject: [PATCH 1/6] Fix instance creation state check --- model_utils/tracker.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/model_utils/tracker.py b/model_utils/tracker.py index 62623fe2..132b1f84 100644 --- a/model_utils/tracker.py +++ b/model_utils/tracker.py @@ -101,7 +101,7 @@ def get_field_value(self, field): return getattr(self.instance, self.field_map[field]) def set_saved_fields(self, fields=None): - if not self.instance.pk: + if self.instance._state.adding: self.saved_data = {} elif fields is None: self.saved_data = self.current() @@ -271,7 +271,7 @@ class ModelInstanceTracker(FieldInstanceTracker): def has_changed(self, field): """Returns ``True`` if field has changed from currently saved value""" - if not self.instance.pk: + if self.instance._state.adding: return True elif field in self.saved_data: return self.previous(field) != self.get_field_value(field) @@ -280,7 +280,7 @@ def has_changed(self, field): def changed(self): """Returns dict of fields that changed since save (with old values)""" - if not self.instance.pk: + if self.instance._state.adding: return {} saved = self.saved_data.items() current = self.current() From 7451055171fedc98c62b48f8eb1bf4420a8ed37b Mon Sep 17 00:00:00 2001 From: MaximZemskov Date: Mon, 27 Jul 2020 23:00:05 +0300 Subject: [PATCH 2/6] Add AUTHORS and CHANGES --- AUTHORS.rst | 1 + CHANGES.rst | 1 + 2 files changed, 2 insertions(+) diff --git a/AUTHORS.rst b/AUTHORS.rst index 64f3b0e5..343bc02e 100644 --- a/AUTHORS.rst +++ b/AUTHORS.rst @@ -49,6 +49,7 @@ | Lucas Wiman | Martey Dodoo | Matthew Schinckel +| Maxim Zemskov | Michael van Tellingen | Mike Bryant | Mikhail Silonov diff --git a/CHANGES.rst b/CHANGES.rst index cd1bb7da..ccbe4201 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -10,6 +10,7 @@ CHANGES - `FieldTracker` now respects `update_fields` changed in overridden `save()` method - Replace ugettext_lazy with gettext_lazy to satisfy Django deprecation warning +- `FieldTracker` now works correct with instance where PK field is not `AutoField` (e.g. `UUIDField`) 4.0.0 (2019-12-11) ------------------ From 27a32a155c83e090c5ea0e0d618571f60bd67a95 Mon Sep 17 00:00:00 2001 From: MaximZemskov Date: Tue, 28 Jul 2020 10:36:05 +0300 Subject: [PATCH 3/6] Fix test --- tests/test_fields/test_field_tracker.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_fields/test_field_tracker.py b/tests/test_fields/test_field_tracker.py index 44449ba0..bd48e3f3 100644 --- a/tests/test_fields/test_field_tracker.py +++ b/tests/test_fields/test_field_tracker.py @@ -790,7 +790,7 @@ class ModelTrackerForeignKeyTests(FieldTrackerForeignKeyTests): tracked_class = ModelTrackedFK def test_custom_without_id(self): - with self.assertNumQueries(2): + with self.assertNumQueries(1): self.tracked_class.objects.get() self.tracker = self.instance.custom_tracker_without_id self.assertChanged() From 92f3308f548432ee73c5f329215fd89f6fa290ed Mon Sep 17 00:00:00 2001 From: MaximZemskov Date: Tue, 28 Jul 2020 12:04:33 +0300 Subject: [PATCH 4/6] Passing tracked field to fields --- tests/models.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/models.py b/tests/models.py index 5d07e52a..0444b37a 100644 --- a/tests/models.py +++ b/tests/models.py @@ -306,7 +306,7 @@ class TrackedMultiple(models.Model): class TrackedFileField(models.Model): some_file = models.FileField(upload_to='test_location') - tracker = FieldTracker() + tracker = FieldTracker(fields=['some_file']) class InheritedTracked(Tracked): From 87271d53e847d68dbf5ecc3477b9712b68bf7b1e Mon Sep 17 00:00:00 2001 From: MaximZemskov Date: Tue, 28 Jul 2020 12:07:59 +0300 Subject: [PATCH 5/6] Revert last commit --- tests/models.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/models.py b/tests/models.py index 0444b37a..5d07e52a 100644 --- a/tests/models.py +++ b/tests/models.py @@ -306,7 +306,7 @@ class TrackedMultiple(models.Model): class TrackedFileField(models.Model): some_file = models.FileField(upload_to='test_location') - tracker = FieldTracker(fields=['some_file']) + tracker = FieldTracker() class InheritedTracked(Tracked): From 05a8fa46111b6d6ac4f68db76b563016dc383aa0 Mon Sep 17 00:00:00 2001 From: MaximZemskov Date: Tue, 28 Jul 2020 12:09:00 +0300 Subject: [PATCH 6/6] Fix previous method --- model_utils/tracker.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/model_utils/tracker.py b/model_utils/tracker.py index 132b1f84..4842effc 100644 --- a/model_utils/tracker.py +++ b/model_utils/tracker.py @@ -140,7 +140,7 @@ def previous(self, field): """Returns currently saved value of given field""" # handle deferred fields that have not yet been loaded from the database - if self.instance.pk and field in self.deferred_fields and field not in self.saved_data: + if not self.instance._state.adding and field in self.deferred_fields and field not in self.saved_data: # if the field has not been assigned locally, simply fetch and un-defer the value if field not in self.instance.__dict__: