Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to get foreign object name #634

Open
Tom5421 opened this issue Apr 23, 2024 · 1 comment
Open

How to get foreign object name #634

Tom5421 opened this issue Apr 23, 2024 · 1 comment
Labels

Comments

@Tom5421
Copy link

Tom5421 commented Apr 23, 2024

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

@alexkiro
Copy link

alexkiro commented May 1, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants