如何在django中选择从另一个模型中键入外键的所有对象?

时间:2011-02-22 17:07:47

标签: python django

对不起标题,我不知道如何表达自己。

例如,我有三个模型:

class Person(models.Model):
    name = models.CharField()

class Teacher(models.Model):
    person = models.ForeignKey(Person)
    subject = models.CharField()

class Student(models.Model):
    person = models.ForeignKey(Person)
    grade = models.CharField()

如何选择教师的所有模型?

1 个答案:

答案 0 :(得分:5)

Person.objects.filter(teacher__isnull=False) 
# return Person who has a teacher pointing to it