django-auditlog icon indicating copy to clipboard operation
django-auditlog copied to clipboard

Reverse relations are being logged

Open aqeelat opened this issue 2 years ago • 5 comments

One issue we came across with auditlog is that we're registering all fields including reverse-related fields. Which means that:

  1. we'll be hitting the db multiple times since these fields might not be pre-fetched in the QuerySet
  2. we'll be updating instance._state.fields_cache as a side effect, which is not a documented behavior.

I will open a PR to soon to show a possible fix for the issue.

aqeelat avatar Jul 25 '23 08:07 aqeelat

@hramezani

Sorry I didn't get around to the pr. The issue is that when we're calling .get_fields() in diff.py, we're getting the auto created fields.

Maybe we can exclude them when we're registering the models.

for field in meta.get_fields():
    if isinstance(field, ManyToManyField):
        m2m_fields.append(field.name)
    elif field.auto_created and isinstance(field, ForeignObjectRel):
        exclude_fields.append(field.name)

aqeelat avatar Aug 13 '23 08:08 aqeelat

@aqeelat can we have a config flag for this? Something like exclude_reverse_relation. The default value can be False to don't change the old behavior.

hramezani avatar Sep 11 '23 14:09 hramezani