Sorting related objects in the Django Admin form interface [duplicate]
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)
class Person(models.Model):
first_name = models.CharField( ... )
last_name = models.CharField( ... )
hero = models.ForeignKey( 'self', null=True, blank=True )
class Meta:
ordering = ['-last_name', 'first_name']
http://docs.djangoproject.com/en/dev/ref/models/options/#ordering
Note: According to the SVN docs, the admin only uses the first of the ordering options, so this is the closest you're gettig to what you want.
精彩评论