使用这样的模型设计:
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
的学生?
答案 0 :(得分:2)
易
school_name = "your school name"
Students_in_school = Student.objects.filter(grade__school__name = school_name)
抱歉,我在巴士上写了这个