I want to abort model deletion from django admin based on some condition.
I tried overriding delete_queryset(self, request, queryset) method.
class MyModelAdmin(ModelAdmin):
def delete_queryset(self, request, queryset):
if not MyModel.objects.filter(is_active=True).exists():
message = "Error message"
raise ValidationError(message)
super().delete_queryset(request, queryset)
This doesn't work since django does not handles exception at this stage.
Found this in Django documentation: https://docs.djangoproject.com/en/4.0/ref/contrib/admin/#django.contrib.admin.ModelAdmin.get_deleted_objects
But I am not sure how can I redirect from here to Django admin page where all model instances are listed and display the error message to user.