开发者

Sorting related objects in the Django Admin form interface [duplicate]

开发者 https://www.devze.com 2022-12-29 12:47 出处:网络
This question already has answers here: Reorder users in django auth 开发者_如何学编程 (2 answers)
This question already has answers here: Reorder users in django auth 开发者_如何学编程 (2 answers) Closed 2 years ago.

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消