如何在django admin中按字段(多级关系)进行过滤?

时间:2015-06-12 19:56:48

标签: python django django-models django-admin

使用这样的模型设计:

class School(models.Model):
    name = models.CharField(max_length=50)

class Grade(models.Model):
    name = models.CharField(max_length=10)
    school = models.ForeignKey(School)

class Student(models.Model):
    name = models.CharField(max_length=100)
    grade = models.ForeignKey(Grade)

由于Student对象与School对象有直接关系,如何过滤django-admin以仅显示所选school的学生?

1 个答案:

答案 0 :(得分:2)

school_name = "your school name"
Students_in_school = Student.objects.filter(grade__school__name = school_name)

抱歉,我在巴士上写了这个