Django admin:根据FK'd属性订购内联

时间:2011-10-07 15:58:41

标签: django django-admin

我有一个简单的Django摄影比赛应用程序,有三个简单的模型定义:

class Person(models.Model):
    name = models.CharField(unique=True, max_length=255)
    id = models.AutoField(primary_key=True)
    class Meta:
        db_table = u'people'
    def __unicode__(self):
        return self.name

class Round(models.Model):
    theme = models.CharField(max_length=255)
    number = models.IntegerField()
    id = models.AutoField(primary_key=True)
    class Meta:
        db_table = u'rounds'
    def __unicode__(self):
        return self.season.name+" - "+self.theme

class Entry(models.Model):
    rank = int
    total_score = models.SmallIntegerField()
    id = models.AutoField(primary_key=True)
    person = models.ForeignKey(Person, db_column='person')
    round = models.ForeignKey(Round, db_column='round')

Round有多个Entry个对象,每个Entry都有一个Person。一个Person显然可以将多个Entrys放入不同的Rounds

在管理视图中,我希望能够选择Round并查看内联Entry项的详细信息。我可以这样做:

class EntryInline(admin.TabularInline):
    model = Entry
    fields = ['comments','person','total_score','image']
    readonly_fields = ['person','image']
    extra = 0

class RoundAdmin(admin.ModelAdmin):
    fields = ['theme','number']
    inlines = [EntryInline]

然而,这似乎任意地对Entry进行排序。我可以在ordering类中使用django的新EntryInline关键字来指定应该在该人身上的排序,但这是由人Id属性而不是他们的name属性排序的

我如何在FK'd Person.name属性中订购此内联?

1 个答案:

答案 0 :(得分:4)

EntryInline课程中添加:

ordering = ["person__name"]