-
-
Notifications
You must be signed in to change notification settings - Fork 7
Description
Describe the bug
The repopulation of the filter input with the previously selected value is broken in certain scenarios.
The input remains empty and shows "All" instead of the expected value. As a consequence the "clear" button is also not shown, which means that there is no way to clear the filter (except to edit the URL directly).
This happens if the selected value is further down the list of possible values than the page size (as defined by django.contrib.admin.views.autocomplete.AutocompleteJsonView.paginate_by which defaults to 20).
To Reproduce
Consider a simple related-entity filter.
- Given the two entities
Parent(with fieldsidandname) andChild(with fieldsid,name, andparent) - Add a DALFRelatedFieldAjax filter on
parentto theChildAdmin - Set
search_fields = ["name", "id"]onParentAdmin - Ensure that there are more than 20 parent entities in the database
- Go to child admin page and pick a parent in the filter that is more than 20 places down
- The filter parent_id=25 (or similar) is appended to the URL
- The page reloads with the filter applied, but the filter input stays empty and shows "ALL"
Expected behavior
If a filter is applied the javascript must execute a search for that value so that it can be displayed in the filter.
Analysis
I think the issue lies here django_admin_list_filter.js#L38.
While an dedicated ajax call is made, it is performed with an empty search term, thus always returning the same top 20 results which are not guaranteed to contain the selected value.
The issue is made slightly more complicated by the fact that the filter value (at least for related entity filters) is the id field which is not necessarily part of the search_fields. Though that could just be stated as a prerequisite.
The most flexible solution would probably be passing an additional parameter to the javascript that specifies which field_name to use when querying based on the lookupValue (i.e. the value of the query parameter).