我有一个简单的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
属性中订购此内联?
答案 0 :(得分:4)
在EntryInline
课程中添加:
ordering = ["person__name"]