diff --git a/.github/workflows/ruff.yml b/.github/workflows/ruff.yml index 459db7f..a25c782 100644 --- a/.github/workflows/ruff.yml +++ b/.github/workflows/ruff.yml @@ -15,7 +15,7 @@ jobs: strategy: max-parallel: 4 matrix: - python-version: ["3.11", "3.12", "3.13"] + python-version: ["3.11", "3.12", "3.13", "3.14"] steps: - name: Check out repository diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index f974491..1470465 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -15,7 +15,7 @@ jobs: strategy: max-parallel: 4 matrix: - python-version: ["3.11", "3.12", "3.13"] + python-version: ["3.11", "3.12", "3.13", "3.14"] steps: - name: Check out repository diff --git a/CHANGELOG.md b/CHANGELOG.md index 9b5db05..6d3092e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,13 @@ # Change Log +**2026-01-24** + +- Fix `DALFRelatedFieldAjax` filter repopulation bug - selected values beyond first 20 results now display correctly on page reload +- Add Python 3.14 to CI workflow matrix +- Upgrade ruff 0.13.3 → 0.14.14 + +--- + **2024-09-06** - Fix dark-mode text color. diff --git a/README.md b/README.md index 61163cd..c92d4bb 100644 --- a/README.md +++ b/README.md @@ -277,6 +277,11 @@ rake upload:test # Upload package to test distro ## Change Log +**2026-01-24** + +- Fix [DALFRelatedFieldAjax filter repopulation bug](https://github.com/vigo/django-admin-list-filter/issues/18) - + selected values beyond pagination limit now display correctly on page reload + **2025-10-06** - [Fix nested FK problem](https://github.com/vigo/django-admin-list-filter/issues/7), thanks to diff --git a/requirements-dev.txt b/requirements-dev.txt index a998c43..1a53612 100644 --- a/requirements-dev.txt +++ b/requirements-dev.txt @@ -3,4 +3,4 @@ pytest==8.4.2 pytest-cov==7.0.0 pytest-django==4.11.1 pytest-factoryboy==2.8.1 -ruff==0.13.3 \ No newline at end of file +ruff==0.14.14 \ No newline at end of file diff --git a/src/dalf/admin.py b/src/dalf/admin.py index 8f7efcc..101f611 100644 --- a/src/dalf/admin.py +++ b/src/dalf/admin.py @@ -86,6 +86,15 @@ def __init__(self, field, request, params, model, model_admin, field_path): selected_value = originial_params.get(self.lookup_kwarg, []) self.selected_value = selected_value[0] if selected_value else None + self.selected_text = None + if self.selected_value: + try: + related_model = field.remote_field.model + obj = related_model.objects.get(pk=self.selected_value) + self.selected_text = str(obj) + except (related_model.DoesNotExist, ValueError): + self.selected_text = str(self.selected_value) + def field_choices(self, _field, _request, _model_admin): return [] @@ -97,4 +106,5 @@ def choices(self, changelist): yield { **self.custom_template_params, 'selected_value': self.selected_value, + 'selected_text': self.selected_text, } diff --git a/src/dalf/static/admin/js/django_admin_list_filter.js b/src/dalf/static/admin/js/django_admin_list_filter.js index 297d0f8..3197a17 100644 --- a/src/dalf/static/admin/js/django_admin_list_filter.js +++ b/src/dalf/static/admin/js/django_admin_list_filter.js @@ -4,12 +4,12 @@ $.fn.djangoAdminListFilterSelect2 = function() { $.each(this, function(i, element) { - const ajaxURL = $(element).data("ajax--url"); const appLabel = element.dataset.appLabel; const modelName = element.dataset.modelName; const fieldName = element.dataset.fieldName; const lookupKwarg = element.dataset.lookupKwarg; - const selectedValue = $(element).prev('.djal-selected-value').val(); + const selectedValue = $(element).prevAll('.djal-selected-value').first().val(); + const selectedText = $(element).prevAll('.djal-selected-text').first().val(); $(element).select2({ ajax: { @@ -35,26 +35,9 @@ window.location.href = navURL.href; }); - if (selectedValue){ - $.ajax({ - url: ajaxURL, - dataType: "json", - data: { - term: "", - app_label: appLabel, - model_name: modelName, - field_name: fieldName - }, - success: function(data){ - if (data.results.length > 0) { - const selectedItem = data.results.find(item => item.id === selectedValue); - if (selectedItem) { - const selectedOption = new Option(selectedItem.text, selectedItem.id, true, true); - $(element).append(selectedOption).trigger("change"); - } - }; - } - }); + if (selectedValue && selectedText) { + const selectedOption = new Option(selectedText, selectedValue, true, true); + $(element).append(selectedOption).trigger("change"); } }); diff --git a/src/dalf/templates/admin/filter/django_admin_list_filter_ajax.html b/src/dalf/templates/admin/filter/django_admin_list_filter_ajax.html index 913a71e..d86c5fd 100644 --- a/src/dalf/templates/admin/filter/django_admin_list_filter_ajax.html +++ b/src/dalf/templates/admin/filter/django_admin_list_filter_ajax.html @@ -8,6 +8,7 @@
  • {% with params=choices|last %} +