Skip to content

Commit

Permalink
Handle query for the audit logs of deleted objects
Browse files Browse the repository at this point in the history
If an object is deleted, the current code cannot find the object or its related objects,
and causes an error in the Audit Log feature when we try to filter that object.
In that case, we directly use the object_id and content_type_id fields of the Audit model
to find it and skip finding the related objects.
  • Loading branch information
hoangpn committed Dec 12, 2024
1 parent f4e6012 commit 7dcf0e7
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion promgen/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,18 @@ def get_queryset(self):

for key in self.FILTERS:
if key in self.request.GET:
obj = self.FILTERS[key].objects.get(pk=self.request.GET[key])
try:
obj = self.FILTERS[key].objects.get(pk=self.request.GET[key])
except self.FILTERS[key].DoesNotExist:
# If we can't find the object (maybe because it was deleted),
# we will search in the audit log by content_type and object_id
# and skip finding the related objects.
queryset = queryset.filter(
object_id=self.request.GET[key],
content_type_id=ContentType.objects.get_for_model(self.FILTERS.get(key)).id,
)
continue

# Get any log entries for the object itself
qset = Q(
object_id=obj.id,
Expand Down

0 comments on commit 7dcf0e7

Please sign in to comment.