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)
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.
Why not at the level model?
class Meta:
ordering = ['field',]
Or at the level admin?
class Admin:
ordering = ('field',)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With