2011-02-22 69 views
2

對不起,我不知道如何表達自己。如何從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() 

我如何可以選擇所有模型是教師

回答

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

千謝謝!這解決了它! –

相關問題