Skip to content Skip to sidebar Skip to footer

Getting All Objects Referenced In A Foreign Key Field

I have models similar to class Person(Model): name = CharField(max_length=100) class Movie(Model): ... director = ForeignKey(Person) How would I get the set of all Pe

Solution 1:

I figured it out,

Person.objects.exclude(director__set=None)

Solution 2:

First you need get the person:

my_person = Person.objects.get(name="XXX")

Then, get all his movies:

person.movie_set.all()

Post a Comment for "Getting All Objects Referenced In A Foreign Key Field"