获取在外键字段中引用的所有对象 [英] Getting all objects referenced in a foreign key field

查看:48
本文介绍了获取在外键字段中引用的所有对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有类似的模型

class Person(Model):
    name = CharField(max_length=100)

class Movie(Model):
    ...
    director = ForeignKey(Person)

如何获取所有设置为任何Movie对象的导演的Person对象的集合?

How would I get the set of all Person objects which are set as the director for any Movie object?

edit:要澄清一下,如果我的电影表"由两个条目组成,一个由导演A组成,一个由导演B组成,而我的人物"表由三个条目A,B和C组成,我想得到集合{A,B}

edit: to clarify, if my Movie 'table' consisted of two entries, one with director A and one with director B, and my Person 'table' consisted of the three entries A, B, and C, I would want to get the set {A, B}

推荐答案

我知道了,

Person.objects.exclude(director__set=None)

这篇关于获取在外键字段中引用的所有对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆