2

I overrode the delete_queryset method to add a condition before being able to delete a model. It looks like this:

def delete_queryset(self, request, queryset):
    if condition_is_not_met:
        self.message_user(request, 'Error', level=messages.ERROR)
        return

    super().delete_queryset(request, queryset)

This DOES prevent the deletion but it shows that error message and below it says, Successfully deleted 'x' objects (Again, not deleted so success message should not be there). I don't want to display that error message and what would be even better is if the confirmation page did not even show up if the condition is not met (This is a bonus though). Any suggestions?

Pittfall
  • 2,751
  • 6
  • 32
  • 61

1 Answers1

3

Just override the has_delete_permission method on modeladmin, which will return True or False based on the condition.

def has_delete_permission(self, request, obj=None):
    if condition_is_not_met:
        return False
    return True

Docs

Mohit Solanki
  • 2,122
  • 12
  • 20