【问题标题】:Django admin: ordering inline based on FK'd propertyDjango admin:基于 FK 属性进行内联排序
【发布时间】:2011-12-03 03:22:51
【问题描述】:

我有一个简单的 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 进行了任意排序。我可以在EntryInline 类中使用django 的新ordering 关键字来指定排序应该在人身上,但是这个命令是由人Id 属性而不是他们的name 属性。

如何在 FK 的 Person.name 属性上订购此内联?

【问题讨论】:

    标签: django django-admin


    【解决方案1】:

    将此添加到EntryInline 类中:

    ordering = ["person__name"]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-23
      • 2021-04-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多