Opened 5 years ago

Closed 4 years ago

Last modified 4 years ago

#30449 closed Bug (fixed)

Ordering problem in admin.RelatedFieldListFilter and admin.RelatedOnlyFieldListFilter

Reported by: Moritz Pfeiffer Owned by: zeynel
Component: contrib.admin Version: dev
Severity: Release blocker Keywords: RelatedFieldListFilter, RelatedOnlyFieldListFilter, ordering
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: yes

Description

RelatedFieldListFilter doesn't fall back to the ordering defined in Model._meta.ordering.

Ordering gets set to an empty tuple in https://github.com/django/django/blob/2.2.1/django/contrib/admin/filters.py#L196 and unless ordering is defined on the related model's ModelAdmin class it stays an empty tuple. IMHO it should fall back to the ordering defined in the related model's Meta.ordering field.

RelatedOnlyFieldListFilter doesn't order the related model at all, even if ordering is defined on the related model's ModelAdmin class.
That's because the call to field.get_choices https://github.com/django/django/blob/2.2.1/django/contrib/admin/filters.py#L422 omits the ordering kwarg entirely.

Attachments (2)

django_filter_ordering.zip (18.5 KB) - added by Moritz Pfeiffer 5 years ago.
Sample project illustrating the problem. Navigate to /admin/foo/book and observer the order of Author in the list filters.
Screenshot 2019-05-06 at 15.49.17.jpg (243.1 KB) - added by Moritz Pfeiffer 5 years ago.
Screenshot of RelatedOnlyFieldListFilter not ordering items.

Download all attachments as: .zip

Change History (15)

Changed 5 years ago by Moritz Pfeiffer

Attachment: django_filter_ordering.zip added

Sample project illustrating the problem. Navigate to /admin/foo/book and observer the order of Author in the list filters.

Changed 5 years ago by Moritz Pfeiffer

Screenshot of RelatedOnlyFieldListFilter not ordering items.

comment:1 Changed 5 years ago by Carlton Gibson

Triage Stage: UnreviewedAccepted

OK, yes, seems a reasonable suggestion if you'd like to work on it.

comment:2 Changed 5 years ago by zeynel

Owner: changed from nobody to zeynel
Status: newassigned

comment:3 Changed 5 years ago by zeynel

Has patch: set

comment:4 Changed 5 years ago by Mariusz Felisiak

Patch needs improvement: set

comment:5 Changed 4 years ago by Althalus

Patch needs improvement: unset

Hello. We've updated our django recently and faced this bug. For me it seems like a quite big regression.
As I see in PR, patch was updated and appropriate tests were added too so I suggest to consider including it in 2.2.4 and backporting to (at least) 2.1.

comment:6 Changed 4 years ago by Mariusz Felisiak

Version: 2.2master

As far as I'm concerned it's not a regression and doesn't qualify for a backport. It's on my list and should be fixed in Django 3.0.

comment:7 Changed 4 years ago by Mariusz Felisiak

Severity: NormalRelease blocker

I closed #30703 as a duplicate. It is a regression introduced in #29835.

comment:8 Changed 4 years ago by Mariusz Felisiak

Alternative PR.

Last edited 4 years ago by Mariusz Felisiak (previous) (diff)

comment:9 Changed 4 years ago by tinodb

I'd argue it is a regression. It worked before and is clearly broken now.

Any workarounds for the moment?

comment:10 in reply to:  9 Changed 4 years ago by Mariusz Felisiak

Replying to tinodb:

I'd argue it is a regression. It worked before and is clearly broken now.

Any workarounds for the moment?

Yes we marked this as a regression and release blocker (please check my previous comment).

comment:11 Changed 4 years ago by Mariusz Felisiak <felisiak.mariusz@…>

Resolution: fixed
Status: assignedclosed

In 00035672:

Fixed #30449 -- Fixed RelatedFieldListFilter/RelatedOnlyFieldListFilter to respect model's Meta.ordering.

Regression in 6d4e5feb79f7eabe8a0c7c4b87f25b1a7f87ca0b.

Co-Authored-By: Mariusz Felisiak <felisiak.mariusz@…>

comment:12 Changed 4 years ago by Mariusz Felisiak <felisiak.mariusz@…>

In 8289fc55:

Refs #30449 -- Made RelatedOnlyFieldListFilter respect ModelAdmin.ordering.

comment:13 Changed 4 years ago by Mariusz Felisiak <felisiak.mariusz@…>

In c2732e6:

[2.2.x] Fixed #30449 -- Fixed RelatedFieldListFilter/RelatedOnlyFieldListFilter to respect model's Meta.ordering.

Regression in 6d4e5feb79f7eabe8a0c7c4b87f25b1a7f87ca0b.

Co-Authored-By: Mariusz Felisiak <felisiak.mariusz@…>

Backport of 00035672a460b6eb5442d2837bc783f8af28c6f3 from master

Note: See TracTickets for help on using tickets.
Back to Top