How to get foreign object name
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.
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.