2👍
✅
Instead of get
, you can use filter
which returns QuerySet
. Unlike the get
method, the filter
does not raise ObjectDoesNotExist
exception, but just returns a empty queryset if there’s no matching object.
Deleting empty queryset has no harm.
So you the line can be replaced with:
Message.objects.filter(id=message_id).delete()
Source:stackexchange.com