Colleen
Colleen

Reputation: 25479

How to use Django foreign key sets in queries?

I have a Question model and a Response model, which contains a foreign key to a Question.

I can get all of the responses for a given question with question.response_set

I want to do something to the effect of

Question.objects.exclude(response_set=None)

What's the best way to go about this?

Upvotes: 1

Views: 179

Answers (1)

Timmy O'Mahony
Timmy O'Mahony

Reputation: 53971

Question.objects.exclude(response__isnull=True)

should return only questions with responses

Upvotes: 4

Related Questions