Changes to `admin.SimpleListFilter` in Django 5.0

Hello all,

I’m upgrading from Django 4.2.8 to 5.0.2.

Please consider the following filter:

from django.contrib import admin


class TestFilter(admin.SimpleListFilter):

    title = "Test"
    parameter_name = "vertrag"

    def lookups(self, request, model_admin):
        return (
            ("ok", "gültig"),
            ("ausg", "ausgeschieden"),
            ("unbek", "unbekannt"),
        )

    def queryset(self, request, queryset):
        # With Django 4.2, this printed "ok", "ausg" or "unbek".
        # With Django 5.0, this prints "k" or "g".
        print(self.value())

        if self.value() == "unbek":
            # Never gets here with Django 5.0.2.
            return queryset.filter(profile__ma__isnull=True)
        else:
            return queryset

With Django 4.2.8, the following test case succeeded:

queryset = User.objects.filter(username="abc")
for value in ("ok", "ausg", "unbek"):
    filt = TestFilter(None, {"vertrag": value}, User, LoriUserAdmin)
    self.assertEqual(
        filt.queryset(None, queryset).count(),
        1 if value == "unbek" else 0,
    )

With Django 5.0.2, I must wrap the value string in a tuple:

    # ...
    filt = TestFilter(None, {"vertrag": (value,)}, User, LoriUserAdmin)
    # ...

Maybe this is related to the item in the release notes about the changes to the …FieldListFilter classes, but I’ve not been able to figure out if this change was introduced intentionally?

If I’m understanding correctly what I’m reading, it appears to me that this was part of the fix for #1873 (Multi-select admin filter for RelatedFields) – Django as applied by Fixed #1873 -- Handled multi-valued query parameters in admin changel… · django/django@d2b688b · GitHub

This also appears to be reflected in the changes made to tests.admin_filters.tests.py

So my guess would be that this is intentional.

Wow, 18 years old. :smile: At first I thought there was a 3 missing between the # and the 1873. :stuck_out_tongue_winking_eye:

It looked like a backwards-incompatible change to me that wasn’t mentioned in the release notes, so initially I thought this was a bug.

@KenWhitesell, thanks for the clarification!

https://code.djangoproject.com/ticket/35173

It was a bug, it should be fixed in the next 5.0 patch release :slight_smile:

That was quick. Thank you! :smiley: