Stop deleting log entries in log_create (#559)

This commit is contained in:
Aleh Rymašeŭski 2023-09-11 14:16:37 +00:00 committed by GitHub
parent af31261946
commit ac737fd55c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 3 additions and 21 deletions

View file

@ -4,6 +4,8 @@
#### Breaking Changes
- feat: stop deleting old log entries when a model with the same pk is created (i.e. the pk value is reused) ([#559](https://github.com/jazzband/django-auditlog/pull/559))
#### Improvements
#### Fixes

View file

@ -66,26 +66,6 @@ class LogEntryManager(models.Manager):
if callable(get_additional_data):
kwargs.setdefault("additional_data", get_additional_data())
# Delete log entries with the same pk as a newly created model.
# This should only be necessary when a pk is used twice.
if kwargs.get("action", None) is LogEntry.Action.CREATE:
if (
kwargs.get("object_id", None) is not None
and self.filter(
content_type=kwargs.get("content_type"),
object_id=kwargs.get("object_id"),
).exists()
):
self.filter(
content_type=kwargs.get("content_type"),
object_id=kwargs.get("object_id"),
).delete()
else:
self.filter(
content_type=kwargs.get("content_type"),
object_pk=kwargs.get("object_pk", ""),
).delete()
# set correlation id
kwargs.setdefault("cid", get_cid())
return self.create(**kwargs)

View file

@ -49,7 +49,7 @@ def log_update(sender, instance, **kwargs):
Direct use is discouraged, connect your model through :py:func:`auditlog.registry.register` instead.
"""
if instance.pk is not None:
if not instance._state.adding:
update_fields = kwargs.get("update_fields", None)
old = sender.objects.filter(pk=instance.pk).first()
_create_log_entry(