开发者

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

开发者 https://www.devze.com 2023-02-13 05:37 出处:网络
Sorry for title, I do not know how else to express myself. For example, I have this three models: class Person(models.Model):

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

精彩评论

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