From 7dcf0e757103c7b9cdf03e2b3a4328d23feeee08 Mon Sep 17 00:00:00 2001 From: hoang Date: Thu, 12 Dec 2024 09:33:38 +0700 Subject: [PATCH] Handle query for the audit logs of deleted objects 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. --- promgen/views.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/promgen/views.py b/promgen/views.py index 33a24eec5..f92c08a37 100644 --- a/promgen/views.py +++ b/promgen/views.py @@ -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,