对不起标题,我不知道如何表达自己。
例如,我有三个模型:
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()
如何选择教师的所有人模型?
答案 0 :(得分:5)
Person.objects.filter(teacher__isnull=False)
# return Person who has a teacher pointing to it