Refs #30449 -- Made RelatedOnlyFieldListFilter respect ModelAdmin.ordering.
This commit is contained in:
parent
00035672a4
commit
8289fc55ff
|
@ -193,11 +193,17 @@ class RelatedFieldListFilter(FieldListFilter):
|
||||||
def expected_parameters(self):
|
def expected_parameters(self):
|
||||||
return [self.lookup_kwarg, self.lookup_kwarg_isnull]
|
return [self.lookup_kwarg, self.lookup_kwarg_isnull]
|
||||||
|
|
||||||
def field_choices(self, field, request, model_admin):
|
def field_admin_ordering(self, field, request, model_admin):
|
||||||
ordering = ()
|
"""
|
||||||
|
Return the model admin's ordering for related field, if provided.
|
||||||
|
"""
|
||||||
related_admin = model_admin.admin_site._registry.get(field.remote_field.model)
|
related_admin = model_admin.admin_site._registry.get(field.remote_field.model)
|
||||||
if related_admin is not None:
|
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)
|
return field.get_choices(include_blank=False, ordering=ordering)
|
||||||
|
|
||||||
def choices(self, changelist):
|
def choices(self, changelist):
|
||||||
|
@ -419,4 +425,5 @@ FieldListFilter.register(lambda f: True, AllValuesFieldListFilter)
|
||||||
class RelatedOnlyFieldListFilter(RelatedFieldListFilter):
|
class RelatedOnlyFieldListFilter(RelatedFieldListFilter):
|
||||||
def field_choices(self, field, request, model_admin):
|
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)
|
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)
|
||||||
|
|
|
@ -741,6 +741,33 @@ class ListFiltersTests(TestCase):
|
||||||
expected = [(self.alfred.pk, 'alfred'), (self.bob.pk, 'bob')]
|
expected = [(self.alfred.pk, 'alfred'), (self.bob.pk, 'bob')]
|
||||||
self.assertEqual(sorted(filterspec.lookup_choices), sorted(expected))
|
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):
|
def test_relatedonlyfieldlistfilter_foreignkey_default_ordering(self):
|
||||||
"""RelatedOnlyFieldListFilter ordering respects Meta.ordering."""
|
"""RelatedOnlyFieldListFilter ordering respects Meta.ordering."""
|
||||||
class BookAdmin(ModelAdmin):
|
class BookAdmin(ModelAdmin):
|
||||||
|
|
Loading…
Reference in New Issue