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

Deferred fields patch #1393

Merged
merged 29 commits into from
Nov 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
00bcd91
added patch and test
JordanHyatt Nov 26, 2022
5fef641
added myself to AUTHORS
JordanHyatt Nov 26, 2022
f3c1864
adde my change to CHANGES.rst
JordanHyatt Nov 26, 2022
df0a2f7
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Nov 26, 2022
c924889
Merge branch 'master' of github.com:JordanHyatt/django-simple-history
JordanHyatt Nov 26, 2022
a22c312
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Nov 26, 2022
a76d619
fixed bug to connect to pre_delete instead of post_delete
JordanHyatt Nov 26, 2022
362e9e9
Merge branch 'master' of github.com:JordanHyatt/django-simple-history
JordanHyatt Nov 26, 2022
53bf721
Merge branch 'master' into master
JordanHyatt Apr 9, 2023
abfb70c
fixed syntax errors in signals.rst and CHANGES.rst
JordanHyatt Sep 14, 2024
99501c9
implemented suggetion made by @jwaschkau in issue #1064
JordanHyatt Sep 16, 2024
24d3181
Merge branch 'master' into deferred-fields-patch
JordanHyatt Sep 16, 2024
12ef23b
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Sep 16, 2024
c20e53a
removed .python-version
JordanHyatt Nov 20, 2024
320f127
added .python-version to the .gitignore
JordanHyatt Nov 20, 2024
8546f6b
changed language_version from 3.8 to 3.11
JordanHyatt Nov 20, 2024
d25365d
moved changes to Unreleased section
JordanHyatt Nov 20, 2024
574a3fe
added reference to GH issue
JordanHyatt Nov 20, 2024
ce52961
removed "s" typo
JordanHyatt Nov 20, 2024
3e21b0a
- added check to signal to see if instance is history enabled before…
JordanHyatt Nov 20, 2024
6c638dd
Merge branch 'master' into deferred-fields-patch
JordanHyatt Nov 20, 2024
aca49da
Clean up the changes doc
tim-schilling Nov 20, 2024
0e7633a
Re-add many to many signals docs
tim-schilling Nov 20, 2024
2150e21
fixed bug of pass insread of return
JordanHyatt Nov 20, 2024
b0f76f7
Merge branch 'deferred-fields-patch' of github.com:JordanHyatt/django…
JordanHyatt Nov 20, 2024
b01aeac
check that fields is "truthy" before calling refresh_from_db
JordanHyatt Nov 20, 2024
08dad4a
added to test to ensure by-pass logic is covered
JordanHyatt Nov 20, 2024
407edbf
fixed bug in test
JordanHyatt Nov 20, 2024
98882ee
Revert pre-commit black version
tim-schilling Nov 20, 2024
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 .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
.idea
.tox/
.venv/
.python-version
/.project
/.pydevproject
/.ve
Expand Down
1 change: 1 addition & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Unreleased
- Made ``skip_history_when_saving`` work when creating an object - not just when
updating an object (gh-1262)
- Improved performance of the ``latest_of_each()`` history manager method (gh-1360)
- Fixed issue with deferred fields causing DoesNotExist error (gh-678)
- Added HistoricOneToOneField (gh-1394)

3.7.0 (2024-05-29)
Expand Down
18 changes: 18 additions & 0 deletions simple_history/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ def finalize(self, sender, **kwargs):
# so the signal handlers can't use weak references.
models.signals.post_save.connect(self.post_save, sender=sender, weak=False)
models.signals.post_delete.connect(self.post_delete, sender=sender, weak=False)
models.signals.pre_delete.connect(self.pre_delete, sender=sender, weak=False)

m2m_fields = self.get_m2m_fields_from_model(sender)

Expand Down Expand Up @@ -668,6 +669,23 @@ def post_delete(self, instance, using=None, **kwargs):
else:
self.create_historical_record(instance, "-", using=using)

def pre_delete(self, instance, **kwargs):
"""
pre_delete method to ensure all deferred fileds are loaded on the model
"""
# First check that history is enabled (on model and globally)
if not getattr(settings, "SIMPLE_HISTORY_ENABLED", True):
return
if not hasattr(instance._meta, "simple_history_manager_attribute"):
return
tim-schilling marked this conversation as resolved.
Show resolved Hide resolved
fields = self.fields_included(instance)
field_attrs = {field.attname for field in fields}
deferred_attrs = instance.get_deferred_fields()
# Load all deferred fields that are present in fields_included
fields = field_attrs.intersection(deferred_attrs)
if fields:
instance.refresh_from_db(fields=fields)

def get_change_reason_for_object(self, instance, history_type, using):
"""
Get change reason for object.
Expand Down
12 changes: 12 additions & 0 deletions simple_history/tests/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -945,6 +945,18 @@ def test_history_with_unknown_field(self):
with self.assertNumQueries(0):
new_record.diff_against(old_record, excluded_fields=["unknown_field"])

def test_delete_with_deferred_fields(self):
Poll.objects.create(question="what's up bro?", pub_date=today)
Poll.objects.create(question="what's up sis?", pub_date=today)
Poll.objects.only("id").first().delete()
Poll.objects.defer("question").all().delete()
# Make sure bypass logic runs
Place.objects.create(name="cool place")
Place.objects.defer("name").first().delete()
with self.settings(SIMPLE_HISTORY_ENABLED=False):
Place.objects.create(name="cool place")
Place.objects.defer("name").all().delete()

def test_history_with_custom_queryset(self):
PollWithQuerySetCustomizations.objects.create(
id=1, pub_date=today, question="Question 1"
Expand Down
Loading