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

Remove first_name / last_name from admin if missing from model #512

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions auditlog/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@
from auditlog.mixins import LogEntryAdminMixin
from auditlog.models import LogEntry

user_model = get_user_model()

user_model_fields = [field.name for field in user_model._meta.get_fields()]

has_first_and_last_name_fields = (
"first_name" in user_model_fields and "last_name" in user_model_fields
)


@admin.register(LogEntry)
class LogEntryAdmin(admin.ModelAdmin, LogEntryAdminMixin):
Expand All @@ -22,10 +30,12 @@ class LogEntryAdmin(admin.ModelAdmin, LogEntryAdminMixin):
"timestamp",
"object_repr",
"changes",
"actor__first_name",
"actor__last_name",
f"actor__{get_user_model().USERNAME_FIELD}",
]
f"actor__{user_model.USERNAME_FIELD}",
] + (
Copy link

@RomHartmann RomHartmann Apr 4, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about something like the following, which adds whichever field is available, handling the case where either fist_name or last_name do actually exist

    search_fields = [
        "timestamp",
        "object_repr",
        "changes",
        f"actor__{get_user_model().USERNAME_FIELD}",
    ] + [f"actor__{f.name}" for f in get_user_model()._meta.get_fields() if f.name in ["first_name", "last_name"]]

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is better because includes whatever fields that are available.
Feel free to create a new PR with tests and changelog

["actor__first_name", "actor__last_name"]
if has_first_and_last_name_fields
else []
)
list_filter = ["action", ResourceTypeFilter, CIDFilter]
readonly_fields = ["created", "resource_url", "action", "user_url", "msg"]
fieldsets = [
Expand Down