I'm trying to return a 405 response from DRF if the object is not allowed to be deleted, but I keep getting a Response 204_NO_CONTENT. Even though the instance is not deleted and the if statement works as should, the returned response is not correct. What am I doing wrong here?
Here's my code:
def perform_destroy(self, instance):
if not instance.deletable:
return Response({'error_message': 'Cannot delete last journal entry line.'}, status=status.HTTP_405_METHOD_NOT_ALLOWED)
sje_id = instance.journal_entry.id
instance.delete()
sje = PurchaseJournalEntry.objects.get(pk=sje_id)
sje.regenerate_mutations()
sje.save()