开发者

How to select all objects that are foreign keyed from another model in django?

Sorry for title, I do not know how else to express myself.

For example, I have this three models:

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(开发者_Python百科Person)
    grade = models.CharField()

How can I select all Person models that are Teachers?


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜