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 Id
property and not their name
property.
How would I order this inline on the FK'd Person.name
property?
将其添加到EntryInline
类中:
ordering = ["person__name"]
链接地址: http://www.djcxy.com/p/65424.html