1
votes

I just implemented a simple listfilter for my Django App, to catch up only active entries like so:

class ActiveAircraftsFilter(SimpleListFilter):
    title = _('aircraft')
    parameter_name = 'aircraft__ac_registration'

    def lookups(self, request, model_admin):
        qs = Aircraft.objects.filter(active=True)

        act = set([ac.ac_registration for ac in qs])
        return [(ac, ac) for ac in act]

    def queryset(self, request, queryset):
        return queryset

This works as expected, but with wrong sorting.

In my dev environment I have three aircrafts, 2 of them active. But in DjangoAdmin the Dropdown contains them in wrong order. I get: D-BBBB D-AAAA

Instead of the expected: D-AAAA and D-BBBB as second.

Interestingly, they are also not sorted by ID...

My underlying Model Aircraft has ordering in the Meta class set. Additionally I also tried setting the ordering while getting the queryset

qs = Aircraft.objects.filter(active=True).order_by('ac_registration')

But this won't help either.

What's wrong with the code above?

1

1 Answers

1
votes

In Python, sets are unordered. You can try

return [(ac, ac) for ac in sorted(act)]