This topic has been discussed a few times before.
See, for example: #10768 (Allow for Admin Actions to be applied to an empty QuerySet) – Django
In the above ticket, the idea of executing actions against an empty QuerySet is dismissed as “bad UI.”
I have used Django for many years now, and my number one frustration is that any time I want to make an action that could optionally take no objects, I have to jump through hoops to achieve it, increasing development time by something like 10x.
Here’s an example from today:
I want to add an action to import a standardized JSON document from a partner’s application into our application. It’s very straightforward. The JSON document represents a “project” with a lot of overlap between our native “projects”.
- If you select two objects, it’s an error: You can’t import a JSON representation of a project into two projects. We message the user to let them know this.
- If you select one object: This is fine. You can update an existing project with a fresh JSON representation from the partner’s app. Any changes will be imported.
- If you select no objects: This is also fine. You can import a project that exists in the partner’s database but not in ours.
The third bullet point is impossible using The Admin. It has been dismissed arbitrarily as something that should never happen, or just bad UX. Yet, it has happened at least a dozen times that I really want to just accept no objects at all, so I have to add all these views, URLs, additional permission checking, etc., just to get this very simple additional use case working, when all it would take on django’s part is to simply allow zero objects to be checked in the admin list view.
Where is the imagination, if the team can’t imagine a scenario where you can execute an action against zero or more objects?
How in the world is this “surprising to the user” and therefore bad? You can just require some selection. In the vast majority of cases, where an empty selection is not applicable, you’re just going to iterate over a query set anyway and end up doing nothing.
If the concern is backward compatibility, why not add something like a no_selection_actions
attribute? Or even a decorator that explicitly registers the action as a “no selection required”?
I’m genuinely wondering if there’s something I don’t understand about why it would be a bad idea to allow actions against empty query sets. It is easily my number one frustration with Django; the one area where, for some reason I don’t understand, the team refuses to be flexible.