* add new setting to activate string repr
* skip using foreign key to construct and display diff of foreign key fields
* concise name and documentation update
# Conflicts:
# docs/source/usage.rst
* add test for enabled setting
* fix code block in docs
* fix version
* add warning to documentation
* Add failing test for diffing polymorphic model instances.
* Make diffing more robust for polymorphic models:
When working with polymorphic models, where a child model inherits from a
parent model, Django's pre_save signal may send model instances in a way
where the log_update() handler receives an instance of the child as the
`old` model, but an instance of the parent as the `new` model.
This leads to a `FieldDoesNotExist` error when a field that only exists on the
child was modified, and `get_field_value()` attempts look up that field on the
parent.
This change makes diffing polymorphic models more robust by considering this
case in `get_default_value()`. Changes to those child fields won't be tracked
in these cases, but at least `django-auditlog` won't prevent the model from
being saved.
* Add CustomLogEntry model support and update tests:
- Added support for CustomLogEntry data model to extend django-auditlog capabilities
- Updated existing test cases to align with new model structure and data handling logic
- Added new test cases to validate CustomLogEntry behavior, model registration, and signal handling
- Ensured backward compatibility with existing LogEntry model where applicable
* Update auditlog/__init__.py
Co-authored-by: Youngkwang Yang <me@youngkwang.dev>
* run only one custom model test matrix (#761)
---------
Co-authored-by: Youngkwang Yang <me@youngkwang.dev>
* Drop Python 3.9 support, set minimum version to 3.10
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Update CHANGELOG.md
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Fix lint error
---------
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
* Add test cases for the `mask_str` function
* Add custom masking function support through mask_callable
* Add test cases for custom masking function
* Update documentation for custom masking function
* fix test case
* rename `AUDITLOG_DEFAULT_MASK_CALLABLE` variable
-AUDITLOG_DEFAULT_MASK_CALLABLE to `AUDITLOG_MASK_CALLABLE`
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Update `CHANGELOG.md` to include mask function customization feature
---------
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
* 'get_field_value' - improve default value handling
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Add Test cases
---------
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
* Branch that implements issue #675, basically, storing the correct JSON type that corresponds to the Python type (None -> null, 1 -> 1', not "1"`).
It's driven by a setting, AUDITLOG_ STORE_JSON_CHANGES , as recommended by @hramezani
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* code formatting tweaks from @hramezani
* increasing test coverage
* added usage for AUDITLOG_STORE_JSON_CHANGES setting
* updated CHANGELOG with info on AUDITLOG_STORE_JSON_CHANGES field
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* added another test for wrong setting type
* should not have committed temporary test changes
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
---------
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
* Fix serialize __proxy__ objects before logging
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Update CHANGELOG.md
---------
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Hasan Ramezani <hasan.r67@gmail.com>
* Audit changes to FK fields when saved using *_id naming.
In Django you can save changes to a FK field on a model using the ID
field name (attname) in addition to the regular FK field name with:
model.related_model_id = 42
model.save(update_fields=["related_model_id'])
or:
model.related_model = related_model
model.save(update_fields=["related_model_id'])
as opposed to the more common:
model.related_model = related_model
model.save(update_fields=["related_model'])
This change ensures those model changes are logged properly.
* Apply suggested change from code review.
* Add a CHANGELOG entry for the fix.
* Change diff to evaluate PKs for FK relationships
The diff method now evaluates the primary keys for changes to determine
if a new LogEntry should be created. Previously, the diff method was
evaluating the string representation of the object. This was flawed
because cases can occur when a parent object has in memory changes to
its string string representation and the child related object is saved
prior to these in memory changes being persisted. In these cases a new
LogEntry object would be created erroneously. This cases is asserted
with a test and a regression test will verify the bug.
The consequence of these updates is that the ``LogEntry.changes`` field
now stores primary keys rather than string representations for related
objects. To keep the changes dictionary display unaffected by this
update, a method was added to the ``LogEntry`` model. This method looks
up the object display string from the stored foreign key. Exceptions
were written to handle backwards compatibility.
* Added test case to cover another bug
Because the string representation is not unique for every object, relying
on it to determine FK diffs may not capture all changes. This test case
shows another type of scenario that is fixed by comparing primary keys
rather than object string representations. This is likely occurring
fairly regularly but is hard to spot because it is an error of omission.
* Update to docstring and added changelog