2

我想按 django 应用程序的管理页面中的非字段列进行过滤。例如,我可以有以下内容:

class Subject(models.Model)
    name = models.CharField(max_length=100)
    def __unicode__(self):
        return self.name

class Student(models.Model)
    name = models.CharField(max_length=100)
    subject = models.ForeignKey(Subject)
    def __unicode__(self):
        return self.name

class School(models.Model)
    school = models.CharField(max_length=100)
    student = models.ForeignKey(Student)

    def subject(self)
        return self.student.subject.name

我将在 admin.py 中有以下内容

class SchoolAdmin(admin.ModelAdmin):
  list_display = ('school', 'student', 'subject')
  list_filter = ('school', 'student', 'subject')

admin.site.register(School, SchoolAdmin)

但是,这不起作用,因为我无法过滤主题,因为它不引用字段?有谁知道这样做的最佳方法?通过四处搜索,看起来 SimpleListFilter 可以解决问题,但我是 python 和 django 的新手,无法弄清楚如何为我的示例实现这一点。

提前致谢

4

1 回答 1

6

尝试这个:

class SchoolAdmin(admin.ModelAdmin):
  list_display = ('school', 'student__name', 'student__subject__name')
  list_filter = ('school', 'student__name', 'student__subject__name')

admin.site.register(School, SchoolAdmin)
于 2013-10-25T13:58:11.980 回答