Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django admin order by alphabet

i am using django-admin, and i have a model as following. it shows as a dropdown list in the admin. how can i order it by alphabet? instead of default user ID?

user= models.ForeignKey(User)
like image 542
Grey Avatar asked Sep 05 '25 03:09

Grey


2 Answers

The problem you describe is, according to Django core devs, a feature, not a bug. In the beginning, Django ordered the User field alphabetically, but this resulted in a performance issue for really big sites (namely Pownce at the time) with hundreds of thousands of users. But instead of asking the few huge Django websites to implement a workaround, the ordering was simply removed, and now every site that has models with a ForeignKey to User has a usability problem.

I posted an ugly workaround on the Django issue tracker about a year ago. Daniel Roseman posted a similar (and IMHO better) solution in another Stackoverflow question.

like image 114
Benjamin Wohlwend Avatar answered Sep 07 '25 21:09

Benjamin Wohlwend


Why not at the level model?

class Meta:
    ordering = ['field',]

Or at the level admin?

class Admin:
    ordering       = ('field',)
like image 22
inigomedina Avatar answered Sep 07 '25 23:09

inigomedina