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

#435 Fix instance creation state check #436

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
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 @@ -49,6 +49,7 @@
| Lucas Wiman <[email protected]>
| Martey Dodoo <[email protected]>
| Matthew Schinckel <[email protected]>
| Maxim Zemskov <[email protected]>
| Michael van Tellingen <[email protected]>
| Mike Bryant <[email protected]>
| Mikhail Silonov <[email protected]>
Expand Down
1 change: 1 addition & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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)
------------------
Expand Down
8 changes: 4 additions & 4 deletions model_utils/tracker.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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__:
Expand Down Expand Up @@ -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)
Expand All @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion tests/test_fields/test_field_tracker.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down