简体   繁体   English

如何在django中选择从另一个模型中键入外键的所有对象?

[英]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.

相关问题 如果是条件,则选择所有django外键模型;如果是另一个条件,则选择部分 - Select all of django foreign key model if condition or part if another condition Django:查询外键控制对象集的属性 - Django: query on properties of set of foreign keyed objects Django 表格 Select 来自另一个 model 没有外键 - Django Form Select From another model without Foreign Key 在Django Admin中将StackedInline外键模型保存在主模型之前 - Saving a StackedInline foreign keyed model before primary model in Django Admin Django:如何通过另一个模型获取与模型相关的所有对象? - Django: How to get all objects related to a model by another model? 如何获取按字段键控的模型对象的字典 - how to get dict of model objects keyed by field Django - 如何从具有外键的 model 中获取所有项目? - Django - How to get all items from a model that has foreign key? 如何在Django中获取子模型中存在外键的父模型的所有对象? - How to get all objects of a Parent model of which foreign key exist in child model in Django? Django:强制一个字段对于具有相同外键的所有模型对象是唯一的 - Django: Force a field to be unique for all model objects with the same foreign key django-如何从另一个进程而不是数据库中获取模型对象 - django - how to get model objects from another process instead of database
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM