Django 4.0 Deleteview changes

by mark | 1 May 2022, 7:34 a.m.

Updated by mark | 7 Jul 2022, 5:10 p.m.

I updated to Django 4.0 and noticed that the custom DeleteView delete() function I had written to deal with comment deletions was no longer being called. Urgh. There was in the logs the rather cryptic message 

DeleteViewCustomDeleteWarning: DeleteView uses FormMixin to handle POST requests. As a consequence, any custom deletion logic in CommentDeleteView.delete() handler should be moved to form_valid().

This turned out to be exactly what I needed to do. I had a def delete(self, request, *args, **kwargs): override that returned HttpResponseRedirect(self.get_success_url()). Changing the function signature to def form_valid(self,form): was all I needed to do. How about that. 

For completeness, I show the amended soft delete function below. This was called delete() prior to 4.0. 

def form_valid(self,form): 
    self.object = self.get_object() 
    self.object.is_deleted = True
    self.object.save() 
    return HttpResponseRedirect(self.get_success_url())

Two ways of handling this. You can define your queryset to filter out is_deleted = True (so your rendering layer can pretend it doesn't exist), or you can skip over the items in the queryset when rendering.

No comments

Back to all articles