Skip to content

Wrong calculation of changes when creating a new record #750

@ts-mk

Description

@ts-mk

I think there is a bug due to the recently introduced setting AUDITLOG_STORE_JSON_CHANGES and the way it works. When creating a new record, the changes field gets populated by fields that did not change.

Environment

Python: 3.12
django-auditlog: 3.2.1

Model

class TestModel(Model):
    text = models.CharField(default="abc")
    time = models.TimeField(null=True, blank=True)

Steps

  1. Set AUDITLOG_STORE_JSON_CHANGES to False.
  2. Create and save a new instance of the model with text="abc", time=None.
  3. auditlog.receivers.log_create calls auditlog.receivers._create_log_entry.
  4. auditlog.diff.model_instance_diff gets called with old being None and new being the new model instance. use_json_for_changes is False.
  5. When old_value and new_value get calculated for the field time, their values are None and "None", which means that they do not match and the field time gets added to the diff variable as ("None", "None"), and then later LogEntry gets created with changes containing the same.

The same problem would happen for the DateField but not for the DateTimeField which has its own handling in auditlog.diff.get_field_value. The handling of that method's return value is inconsistent. Sometimes smart_str is applied, sometimes not.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions