Compare commits

...

5 commits

Author SHA1 Message Date
peymanahmadifar
5a78bd331e
Merge 952924f4cf into 0e3a2ec1a7 2026-03-13 08:17:09 +00:00
pre-commit-ci[bot]
0e3a2ec1a7
[pre-commit.ci] pre-commit autoupdate (#808)
Some checks failed
Test / SQLite • Python 3.10 (push) Has been cancelled
Test / SQLite • Python 3.11 (push) Has been cancelled
Test / SQLite • Python 3.12 (push) Has been cancelled
Test / SQLite • Python 3.13 (push) Has been cancelled
Test / PostgreSQL • Python 3.10 (push) Has been cancelled
Test / PostgreSQL • Python 3.11 (push) Has been cancelled
Test / PostgreSQL • Python 3.12 (push) Has been cancelled
Test / PostgreSQL • Python 3.13 (push) Has been cancelled
Test / MySQL • Python 3.10 (push) Has been cancelled
Test / MySQL • Python 3.11 (push) Has been cancelled
Test / MySQL • Python 3.12 (push) Has been cancelled
Test / MySQL • Python 3.13 (push) Has been cancelled
updates:
- [github.com/psf/black-pre-commit-mirror: 26.1.0 → 26.3.0](https://github.com/psf/black-pre-commit-mirror/compare/26.1.0...26.3.0)

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
2026-03-10 10:58:29 +01:00
pre-commit-ci[bot]
dfd5b79d2d
[pre-commit.ci] pre-commit autoupdate (#807)
Some checks failed
Test / SQLite • Python 3.10 (push) Has been cancelled
Test / SQLite • Python 3.11 (push) Has been cancelled
Test / SQLite • Python 3.12 (push) Has been cancelled
Test / SQLite • Python 3.13 (push) Has been cancelled
Test / PostgreSQL • Python 3.10 (push) Has been cancelled
Test / PostgreSQL • Python 3.11 (push) Has been cancelled
Test / PostgreSQL • Python 3.12 (push) Has been cancelled
Test / PostgreSQL • Python 3.13 (push) Has been cancelled
Test / MySQL • Python 3.10 (push) Has been cancelled
Test / MySQL • Python 3.11 (push) Has been cancelled
Test / MySQL • Python 3.12 (push) Has been cancelled
Test / MySQL • Python 3.13 (push) Has been cancelled
updates:
- [github.com/PyCQA/isort: 8.0.0 → 8.0.1](https://github.com/PyCQA/isort/compare/8.0.0...8.0.1)
- [github.com/adamchainz/django-upgrade: 1.29.1 → 1.30.0](https://github.com/adamchainz/django-upgrade/compare/1.29.1...1.30.0)

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
2026-03-04 13:06:54 +01:00
pre-commit-ci[bot]
952924f4cf [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
2025-10-09 10:45:55 +00:00
peyman
ee278edc8f Add option to exclude reverse relations from model diffs
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 #551 (or: Fixes behavior described in jazzband/django-auditlog#551).
2025-10-09 14:11:21 +03:30
4 changed files with 31 additions and 3 deletions

View file

@ -1,7 +1,7 @@
---
repos:
- repo: https://github.com/psf/black-pre-commit-mirror
rev: 26.1.0
rev: 26.3.0
hooks:
- id: black
language_version: python3.10
@ -14,7 +14,7 @@ repos:
- id: flake8
args: ["--max-line-length", "110"]
- repo: https://github.com/PyCQA/isort
rev: 8.0.0
rev: 8.0.1
hooks:
- id: isort
- repo: https://github.com/asottile/pyupgrade
@ -23,7 +23,7 @@ repos:
- id: pyupgrade
args: [--py310-plus]
- repo: https://github.com/adamchainz/django-upgrade
rev: 1.29.1
rev: 1.30.0
hooks:
- id: django-upgrade
args: [--target-version, "4.2"]

View file

@ -21,6 +21,11 @@ settings.AUDITLOG_EXCLUDE_TRACKING_FIELDS = getattr(
settings, "AUDITLOG_EXCLUDE_TRACKING_FIELDS", ()
)
# Exclude reverse relation fields across all models
settings.AUDITLOG_EXCLUDE_REVERSE_RELATIONS = getattr(
settings, "AUDITLOG_EXCLUDE_REVERSE_RELATIONS", False
)
# Mask named fields across all models
settings.AUDITLOG_MASK_TRACKING_FIELDS = getattr(
settings, "AUDITLOG_MASK_TRACKING_FIELDS", ()

View file

@ -218,6 +218,19 @@ def model_instance_diff(
fields = set()
model_fields = None
# Optionally exclude reverse relations (auto-created fields).
# 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:
def is_reverse_field(f):
return getattr(f, "auto_created", False) and not getattr(
f, "concrete", False
)
fields = {f for f in fields if not is_reverse_field(f)}
if fields_to_check:
fields = {
field

View file

@ -676,3 +676,13 @@ The mixin provides the following configuration options:
- ``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
Default: False
Use ``AUDITLOG_EXCLUDE_REVERSE_RELATIONS`` to exclude reverse relation fields (auto-created fields
where `field.auto_created is True` and `field.concrete is False`) when computing
model diffs. This avoids accidental database queries for related objects and avoids
mutating `instance._state.fields_cache` as a side-effect.
Added to address: https://github.com/jazzband/django-auditlog/issues/551