1

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 Person objects which are set as the director for any Movie object?

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}

4

2 に答える 2

2

私はそれを考え出した、

Person.objects.exclude(director__set=None)
于 2012-07-12T03:39:43.107 に答える
1

まず、人を取得する必要があります:

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

次に、彼のすべての映画を取得します。

person.movi​​e_set.all()

于 2012-07-12T01:33:12.120 に答える