开发者

Django admin: ordering inline based on FK'd property

I've got a simple Django photography competition app that has three simple model definitions:

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')

A Round has multiple Entry objects, and each Entry has one Person. One Person can obviously have multiple Entrys into different Rounds.

In the admin view, I'd like to be able to select a Round and see the details of the Entry items inline. This I can do with the following:

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]

However, this sorts the Entry seemingly arbitrarily. I can use django's new ordering keyword in the EntryInline class to specify the ordering should be on the person, but this orders by the person 开发者_Python百科Id property and not their name property.

How would I order this inline on the FK'd Person.name property?


Add this in the EntryInline class:

ordering = ["person__name"]
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜