[英]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(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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.