Skip to content

[BUG] Repopulation of filter input broken for DALFRelatedFieldAjax #18

@penenkel

Description

@penenkel

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 fields id and name ) and Child (with fields id, name , and parent)
  • Add a DALFRelatedFieldAjax filter on parent to the ChildAdmin
  • Set search_fields = ["name", "id"] on ParentAdmin
  • 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).

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions