asaji
asaji

Reputation: 398

Field lookup with a queryset

Hi guys i've been trying to figure out this problem for an hour or two and it's got me stumped.

So basically I have 3 models, A,B,C, all linked together using foreign keys.

b = B.objects.filter(resident=request.user)
c = C.objects.filter(todays_treatment=medication,patient=b)

however c won't give me any values except for the results of the first object returned in query b. I understand this is due to b being returned as a queryset, but I don't understand how I should be using it then? How can I get c to be the results for ALL of the objects in query b? How do you guys normally "follow" foreign key relationships? I've tried that select_related() function but it won't do what I want it to do. Please help!!

Upvotes: 0

Views: 803

Answers (1)

You're probably looking for the __in syntax:

c = C.objects.filter(todays_treatment=medication, patient__in=b)

To get all C objects where the patient is in the b queryset, assuming B objects are what C.patient points to.

Upvotes: 4

Related Questions