Opened 8 years ago

Closed 8 years ago

#10451 closed (invalid)

Admin site dropdown ordering for ForeignKey doesn't work as expected

Reported by: Leo Shklovskii Owned by: nobody
Component: contrib.admin Version: master
Severity: Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

In the case of:

from django.contrib.auth.models import User

class Foo(models.Model):
    user = models.ForeignKey(User)

When the admin page for Foo is rendered, the select dropdown to choose users has them sorted by id, despite the fact that the UserAdmin class has:

    ordering = ('username',)

I can get around this for my own models by setting ordering on the model itself but I don't want to modify or extend the existing User model and it seems reasonable for the admin site to respect the default ordering as specified in the ModelAdmin class.

Change History (4)

comment:1 Changed 8 years ago by Alex Gaynor

Needs documentation: unset
Needs tests: unset
Patch needs improvement: unset
Resolution: invalid
Status: newclosed

You can change the ordering by overiding formfield_for_foreignkey on the ModelAdmin in django trunk.

comment:2 Changed 8 years ago by Leo Shklovskii

Resolution: invalid
Status: closedreopened

Thanks Adam, that's a great workaround, but are you also making the design decision that the ordering specified in the admin class shouldn't apply by default? That seems a little strange.

comment:3 Changed 8 years ago by Leo Shklovskii

Er, I meant Alex, sorry about that.

comment:4 Changed 8 years ago by Alex Gaynor

Resolution: invalid
Status: reopenedclosed

No, there is no precedent anywhere in the admin for a different ModelAdmin controlling something in one ModelAdmin. It uses the default ordering on the related model by default.

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