Wagh
Wagh

Reputation: 4306

get the objects of model refrenced in another model as fk django

How to get list of objects who has foriegn key in another model

Ex:
class A:
     field1
     field2

class B:
     field1 = fk to A
     field2

i want to get the list of objects of class A who has fk objects present in class B. i dont want to get objects for class A whose fk is not available in class B. And is it possible to get in one query?

Upvotes: 0

Views: 24

Answers (1)

brunodea
brunodea

Reputation: 220

I can't test it right now but probably something like:

as_in_bs = map(lambda b: b.field1, B.objects.filter(field1__isnull=False))

Take a look in their docs for more ideas.

Upvotes: 1

Related Questions