Skip to content

Commit

Permalink
Set history delete_related to False by default (jazzband#557)
Browse files Browse the repository at this point in the history
This is a breaking change. The default behavior is to delete the
related objects, but it doesn't make sense to apply it to audit log
entries, at least not by default.
  • Loading branch information
aleh-rymasheuski committed Sep 1, 2023
1 parent 47188b4 commit ab65364
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 20 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#### 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))
- feat: Set `AuditlogHistoryField.delete_related` to `False` by default. This is different from the default configuration of Django's `GenericRelation`, but we should not erase the audit log of objects on deletion by default. ([#557](https://github.com/jazzband/django-auditlog/pull/557))

#### Improvements

Expand Down
8 changes: 3 additions & 5 deletions auditlog/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -503,14 +503,12 @@ class AuditlogHistoryField(GenericRelation):
:param pk_indexable: Whether the primary key for this model is not an :py:class:`int` or :py:class:`long`.
:type pk_indexable: bool
:param delete_related: By default, including a generic relation into a model will cause all related
objects to be cascade-deleted when the parent object is deleted. Passing False to this overrides this
behavior, retaining the full auditlog history for the object. Defaults to True, because that's
Django's default behavior.
:param delete_related: Delete referenced auditlog entries together with the tracked object.
Defaults to False to keep the integrity of the auditlog.
:type delete_related: bool
"""

def __init__(self, pk_indexable=True, delete_related=True, **kwargs):
def __init__(self, pk_indexable=True, delete_related=False, **kwargs):
kwargs["to"] = LogEntry

if pk_indexable:
Expand Down
30 changes: 15 additions & 15 deletions auditlog_tests/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class SimpleModel(models.Model):
integer = models.IntegerField(blank=True, null=True)
datetime = models.DateTimeField(auto_now=True)

history = AuditlogHistoryField()
history = AuditlogHistoryField(delete_related=True)


class AltPrimaryKeyModel(models.Model):
Expand All @@ -35,7 +35,7 @@ class AltPrimaryKeyModel(models.Model):
integer = models.IntegerField(blank=True, null=True)
datetime = models.DateTimeField(auto_now=True)

history = AuditlogHistoryField(pk_indexable=False)
history = AuditlogHistoryField(delete_related=True, pk_indexable=False)


class UUIDPrimaryKeyModel(models.Model):
Expand All @@ -50,7 +50,7 @@ class UUIDPrimaryKeyModel(models.Model):
integer = models.IntegerField(blank=True, null=True)
datetime = models.DateTimeField(auto_now=True)

history = AuditlogHistoryField(pk_indexable=False)
history = AuditlogHistoryField(delete_related=True, pk_indexable=False)


class ProxyModel(SimpleModel):
Expand Down Expand Up @@ -80,7 +80,7 @@ class RelatedModel(RelatedModelParent):
to="SimpleModel", on_delete=models.CASCADE, related_name="reverse_one_to_one"
)

history = AuditlogHistoryField()
history = AuditlogHistoryField(delete_related=True)


class ManyRelatedModel(models.Model):
Expand All @@ -91,7 +91,7 @@ class ManyRelatedModel(models.Model):
recursive = models.ManyToManyField("self")
related = models.ManyToManyField("ManyRelatedOtherModel", related_name="related")

history = AuditlogHistoryField()
history = AuditlogHistoryField(delete_related=True)

def get_additional_data(self):
related = self.related.first()
Expand All @@ -103,7 +103,7 @@ class ManyRelatedOtherModel(models.Model):
A model related to ManyRelatedModel as many-to-many.
"""

history = AuditlogHistoryField()
history = AuditlogHistoryField(delete_related=True)


@auditlog.register(include_fields=["label"])
Expand All @@ -115,7 +115,7 @@ class SimpleIncludeModel(models.Model):
label = models.CharField(max_length=100)
text = models.TextField(blank=True)

history = AuditlogHistoryField()
history = AuditlogHistoryField(delete_related=True)


class SimpleExcludeModel(models.Model):
Expand All @@ -126,7 +126,7 @@ class SimpleExcludeModel(models.Model):
label = models.CharField(max_length=100)
text = models.TextField(blank=True)

history = AuditlogHistoryField()
history = AuditlogHistoryField(delete_related=True)


class SimpleMappingModel(models.Model):
Expand All @@ -138,7 +138,7 @@ class SimpleMappingModel(models.Model):
vtxt = models.CharField(verbose_name="Version", max_length=100)
not_mapped = models.CharField(max_length=100)

history = AuditlogHistoryField()
history = AuditlogHistoryField(delete_related=True)


@auditlog.register(mask_fields=["address"])
Expand All @@ -150,7 +150,7 @@ class SimpleMaskedModel(models.Model):
address = models.CharField(max_length=100)
text = models.TextField()

history = AuditlogHistoryField()
history = AuditlogHistoryField(delete_related=True)


class AdditionalDataIncludedModel(models.Model):
Expand All @@ -163,7 +163,7 @@ class AdditionalDataIncludedModel(models.Model):
text = models.TextField(blank=True)
related = models.ForeignKey(to=SimpleModel, on_delete=models.CASCADE)

history = AuditlogHistoryField()
history = AuditlogHistoryField(delete_related=True)

def get_additional_data(self):
"""
Expand All @@ -190,7 +190,7 @@ class DateTimeFieldModel(models.Model):
time = models.TimeField()
naive_dt = models.DateTimeField(null=True, blank=True)

history = AuditlogHistoryField()
history = AuditlogHistoryField(delete_related=True)


class ChoicesFieldModel(models.Model):
Expand All @@ -212,7 +212,7 @@ class ChoicesFieldModel(models.Model):
status = models.CharField(max_length=1, choices=STATUS_CHOICES)
multiplechoice = models.CharField(max_length=255, choices=STATUS_CHOICES)

history = AuditlogHistoryField()
history = AuditlogHistoryField(delete_related=True)


class CharfieldTextfieldModel(models.Model):
Expand All @@ -225,7 +225,7 @@ class CharfieldTextfieldModel(models.Model):
longchar = models.CharField(max_length=255)
longtextfield = models.TextField()

history = AuditlogHistoryField()
history = AuditlogHistoryField(delete_related=True)


class PostgresArrayFieldModel(models.Model):
Expand All @@ -247,7 +247,7 @@ class PostgresArrayFieldModel(models.Model):
models.CharField(max_length=1, choices=STATUS_CHOICES), size=3
)

history = AuditlogHistoryField()
history = AuditlogHistoryField(delete_related=True)


class NoDeleteHistoryModel(models.Model):
Expand Down

0 comments on commit ab65364

Please sign in to comment.