1

How do you manually audit log in django-auditlog? There is no example in the documentation and there is still very limited sources about this. Should the model be registered? What i am really trying to achieve is to manually log data so that i can add additional data like remarks. But i am having problems in using the LogEntry.objects.log_create().

Thanks in advance.

1 Answers1

1

first import LogEntry model from auditlog package:

from auditlog.models import LogEntry

second add you custom log with fields used below:

my_object = MyModel.objects.get(id=object_id)

LogEntry.objects.create(
            actor_id=request.user.id,
            content_type_id=ContentType.objects.get_for_model(my_object).pk,
            object_id=object_id,
            object_pk=object_id,
            object_repr=str(my_object),
            action=1,  # 0 for create, 1 for update, 2 for delete
            changes=json.dumps({
                "field_name1": ["from_value", "to_value"],
                "field_name2": ["from_value", "to_value"]
            }),
)