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

How to get foreign object name

Open Tom5421 opened this issue 1 year ago • 1 comments

Hi,

I have implemented the Audit Log on one of my Django projects, I'm currently stuck at trying to get the foreign objects name for a audit log of user groups.

A user can be in either Group A, Group B or Group C. They can only be in 1 group at a time and this is just a foreign key field on my user model. I would like to retrieve the group name as the ID by its self is not very user friendly.

Would anyone be able to give me some guidance on how I could achieve this? This is not the only field that will change on the user model, as I will also be tracking email changes and a few more fields.

image

Tom5421 avatar Apr 23 '24 17:04 Tom5421

Assuming you have the log entry itself, you can do something like:

if "group" in log_entry.changes:
    old_value = Group.objects.get(pk=log_entry.changes[0])
    new_value = Group.objects.get(pk=log_entry.changes[1])

Where log_entry is the instance of auditlog.models.LogEntry that you are inspecting.

alexkiro avatar May 01 '24 07:05 alexkiro