Sorting related objects in the Django Admin form interface

Posted by Carver on Stack Overflow See other posts from Stack Overflow or by Carver
Published on 2010-05-09T15:19:42Z Indexed on 2010/05/09 15:28 UTC
Read the original article Hit count: 188

Filed under:
|

I am looking to sort the related objects that show up when editing an object using the admin form. So for example, I would like to take the following object:

class Person(models.Model):
    first_name = models.CharField( ... )
    last_name = models.CharField( ... )
    hero = models.ForeignKey( 'self', null=True, blank=True )

and edit the first name, last name and hero using the admin interface. I want to sort the objects as they show up in the drop down by last name, first name (ascending). How do I do that?

Context

  • I'm using Django v1.1.
  • I started by looking for help in the django admin docs, but didn't find the solution
  • As you can see in the example, the foreign key is pointing to itself, but I expect it would be the same as pointing to a different model object.
  • Bonus points for being able to filter the related objects, too (eg~ only allow selecting a hero with the same first name)

© Stack Overflow or respective owner

Related posts about django

Related posts about django-admin