diff --git a/django/contrib/admin/filters.py b/django/contrib/admin/filters.py index d65e01d5e2f..a9e5563c6cb 100644 --- a/django/contrib/admin/filters.py +++ b/django/contrib/admin/filters.py @@ -193,11 +193,17 @@ class RelatedFieldListFilter(FieldListFilter): def expected_parameters(self): return [self.lookup_kwarg, self.lookup_kwarg_isnull] - def field_choices(self, field, request, model_admin): - ordering = () + def field_admin_ordering(self, field, request, model_admin): + """ + Return the model admin's ordering for related field, if provided. + """ related_admin = model_admin.admin_site._registry.get(field.remote_field.model) if related_admin is not None: - ordering = related_admin.get_ordering(request) + return related_admin.get_ordering(request) + return () + + def field_choices(self, field, request, model_admin): + ordering = self.field_admin_ordering(field, request, model_admin) return field.get_choices(include_blank=False, ordering=ordering) def choices(self, changelist): @@ -419,4 +425,5 @@ FieldListFilter.register(lambda f: True, AllValuesFieldListFilter) class RelatedOnlyFieldListFilter(RelatedFieldListFilter): def field_choices(self, field, request, model_admin): pk_qs = model_admin.get_queryset(request).distinct().values_list('%s__pk' % self.field_path, flat=True) - return field.get_choices(include_blank=False, limit_choices_to={'pk__in': pk_qs}) + ordering = self.field_admin_ordering(field, request, model_admin) + return field.get_choices(include_blank=False, limit_choices_to={'pk__in': pk_qs}, ordering=ordering) diff --git a/tests/admin_filters/tests.py b/tests/admin_filters/tests.py index 75563bbaaf7..4e6f1f4732c 100644 --- a/tests/admin_filters/tests.py +++ b/tests/admin_filters/tests.py @@ -741,6 +741,33 @@ class ListFiltersTests(TestCase): expected = [(self.alfred.pk, 'alfred'), (self.bob.pk, 'bob')] self.assertEqual(sorted(filterspec.lookup_choices), sorted(expected)) + def test_relatedonlyfieldlistfilter_foreignkey_ordering(self): + """RelatedOnlyFieldListFilter ordering respects ModelAdmin.ordering.""" + class EmployeeAdminWithOrdering(ModelAdmin): + ordering = ('name',) + + class BookAdmin(ModelAdmin): + list_filter = ( + ('employee', RelatedOnlyFieldListFilter), + ) + + albert = Employee.objects.create(name='Albert Green', department=self.dev) + self.djangonaut_book.employee = albert + self.djangonaut_book.save() + self.bio_book.employee = self.jack + self.bio_book.save() + + site.register(Employee, EmployeeAdminWithOrdering) + self.addCleanup(lambda: site.unregister(Employee)) + modeladmin = BookAdmin(Book, site) + + request = self.request_factory.get('/') + request.user = self.alfred + changelist = modeladmin.get_changelist_instance(request) + filterspec = changelist.get_filters(request)[0][0] + expected = [(albert.pk, 'Albert Green'), (self.jack.pk, 'Jack Red')] + self.assertEqual(filterspec.lookup_choices, expected) + def test_relatedonlyfieldlistfilter_foreignkey_default_ordering(self): """RelatedOnlyFieldListFilter ordering respects Meta.ordering.""" class BookAdmin(ModelAdmin):