Add AUDITLOG_EXCLUDE_REVERSE_RELATIONS to avoid logging reverse relation fields#768
Open
peymanahmadifar wants to merge 2 commits intojazzband:masterfrom
Open
Conversation
Add AUDITLOG_EXCLUDE_REVERSE_RELATIONS setting which, when True, excludes auto-created reverse relation fields (auto_created and not concrete) from the diff calculation to avoid extra DB hits and inadvertent mutation of instance._state.fields_cache. Closes jazzband#551 (or: Fixes behavior described in jazzband#551).
for more information, see https://pre-commit.ci
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #768 +/- ##
==========================================
- Coverage 95.81% 95.58% -0.24%
==========================================
Files 34 34
Lines 1195 1200 +5
==========================================
+ Hits 1145 1147 +2
- Misses 50 53 +3 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Member
|
Thanks @peymanahmadifar for the PR. Please add tests and a changelog entry. @2ykwang Please review the PR |
2ykwang
requested changes
Oct 18, 2025
Member
2ykwang
left a comment
There was a problem hiding this comment.
thanks for the patch! @peymanahmadifar
could you add test cases for this change?
| - ``auditlog_history_template``: Template to use for rendering the history page (default: ``auditlog/object_history.html``) | ||
| - ``auditlog_history_per_page``: Number of log entries to display per page (default: 10) | ||
|
|
||
| .. versionadded:: 3.2.2 |
Member
There was a problem hiding this comment.
Suggested change
| .. versionadded:: 3.2.2 | |
| .. versionadded:: 3.4.0 |
| # Reverse relations (auto_created & not concrete) can cause unwanted DB hits | ||
| # and mutate instance._state.fields_cache as a side-effect. | ||
| # Make this behavior opt-in via AUDITLOG_EXCLUDE_REVERSE_RELATIONS. | ||
| if settings.AUDITLOG_EXCLUDE_REVERSE_RELATIONS: |
Member
There was a problem hiding this comment.
I think the reverse-relation filtering belongs in track_field rather than model_instance_diff
That keeps the “what to track” policy in one place
def track_field(field):
"""
Returns whether the given field should be tracked by Auditlog.
Untracked fields are many-to-many relations and relations to the Auditlog LogEntry model.
:param field: The field to check.
:type field: Field
:return: Whether the given field should be tracked.
:rtype: bool
"""
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes issue #551 — Reverse relations are being logged.
Summary:
When computing model diffs we used Model._meta.get_fields() which includes
auto-created reverse relation fields. Those fields can trigger extra DB queries
(if not prefetched) and mutate instance._state.fields_cache as a side-effect.
This PR:
AUDITLOG_EXCLUDE_REVERSE_RELATIONS(default False).from the fields used to calculate diffs.
Notes:
Please let me know if you'd prefer to make this opt-out instead of opt-in,
or if you'd rather that the default behavior be changed. Happy to update the PR.