Opened 6 years ago

Closed 6 years ago

#10451 closed (invalid)

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

Reported by: Leo 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 6 years ago by Alex

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Resolution set to invalid
  • Status changed from new to closed

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

comment:2 Changed 6 years ago by Leo

  • Resolution invalid deleted
  • Status changed from closed to reopened

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 6 years ago by Leo

Er, I meant Alex, sorry about that.

comment:4 Changed 6 years ago by Alex

  • Resolution set to invalid
  • Status changed from reopened to closed

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